Excel 图表范围基于另一个单元格中的值 sheet

Excel Chart range based on the value in a cell in another sheet

我有一个 sheet(仪表板)有多个帕累托图,另一个 sheet(数据)通过标准 $A$1:$B$2 中的公式引入每个图表的范围格式。

如何在“仪表板”的帕累托图中使用 Sheet“数据”中的这些范围? 图表名称在数据 B4 中 图表范围在数据 C4 中 我有用于故障排除的每个图表的代码,下面是一个来自单个图表的代码

Sub FirstChart()
    Dim FirstChartName As String
    Dim FirstChartRange As Range
    
        FirstChartName = Sheets("Data").Range("B4")
        Set FirstChartRange = Worksheets("Data").Range(Sheets("Data").Range("C4").Value)
        Sheets("Dashboard").ChartObjects("FirstChart").Activate
        ActiveChart.ChartArea.Select
        ActiveChart.HasTitle = True
        ActiveChart.ChartTitle.Text = FirstChartName 
        ActiveChart.SetSourceData Source:=FirstChartRange
End Sub

提前致谢。

更新: 感谢@coross24 和@WIL。 我已经根据他们对 https://gofile.io/d/8HfjQv

的回答上传了文件

您在引用 FirstChartRange 参数时似乎有点不对劲。由于变量被绑定为范围,您所做的是将单元格 C4 引用为 运行ge,而不是 within 中的字符串 运行ge ,然后尝试绘制该单元格中的字符串值!当运行你的代码,我运行进入类型错误。
我已经修改了上面的代码并在我的工作簿中的单个图表上对其进行了测试,它似乎工作正常。我还提前绑定了您的工作表,这样您就不必在代码中重复自己了。

Sub FirstChart()
    Dim FirstChartName As String
    Dim FirstChartRange As String
    Dim shtData As Excel.Worksheet
    Dim shtDashboard As Excel.Worksheet
    Dim chart As Excel.chart
    
    Set shtData = ThisWorkbook.Sheets("Data")
    Set shtDashboard = ThisWorkbook.Sheets("Dashboard")
    
    ' get chart name
    FirstChartName = shtData.Range("B4").Value2
    ' get chart range
    FirstChartRange = shtData.Range("C4").Value2
    ' change data for first chart
    Set chart = shtDashboard.ChartObjects("FirstChart").chart
    With chart
        .HasTitle = True
        .ChartTitle.Text = FirstChartName
        .SetSourceData shtData.Range(FirstChartRange)
    End With
       
End Sub

祝你好运!

试试这个

Sub FirstChart()

Dim FirstChartName As String
Dim FirstChartRange As String
Dim shtData As Excel.Worksheet
Dim shtDashboard As Excel.Worksheet
Dim chart As Excel.chart

Set shtData = ThisWorkbook.Sheets("Data")
Set shtDashboard = ThisWorkbook.Sheets("Dashboard")

' get chart name
FirstChartName = shtData.Range("B4").Value2
' get chart range
FirstChartRange = shtData.Range("C4").Value2
' change data for first chart
Set chart = shtDashboard.ChartObjects("FirstChart").chart
With chart
    .HasTitle = True
    .ChartTitle.Text = FirstChartName
    .SetSourceData FirstChartRange
End With
   
End Sub

这是创建散点图的示例。 它应该让你去。 根据您的需要进行调整。

Sub CreateChart()
    Dim wscharts As Worksheet, wsdata As Worksheet
    Set wscharts = Worksheets("Dashboard")
    Set wsdata = Worksheets("Data")
    Dim sh As Shape
    Set sh = wscharts.Shapes.AddChart2(240, xlXYScatterLines)
    sh.Select
    Dim rngText As String
    rngText = wsdata.Name & "!" & wsdata.Range("Data!$C").Value
    ActiveChart.SetSourceData Source:=Range(rngText)
    sh.Name = wsdata.Range("Data!$B").Value
End Sub

如图所示,它可以很好地处理数据

宗教,
我不得不 post 另一个答案,因为我的声誉不够高,无法回复评论。有一个绝对肮脏的解决方法……似乎数据确实填充了图形,您只是绕过错误消息,然后将 y-axis 比例设置为自动。代码见下方:

Option Explicit

Sub FirstChart()
    Dim FirstChartName As String
    Dim FirstChartRange As String
    Dim rng As Range
    Dim r As Range
    Dim shtData As Excel.Worksheet
    Dim shtDashboard As Excel.Worksheet
    Dim chart As Excel.chart
    Dim tmp As Variant

    Set shtData = ThisWorkbook.Sheets("Data")
    Set shtDashboard = ThisWorkbook.Sheets("Dashboard")
    
    ' get chart name
    FirstChartName = shtData.Range("B4").Value2
    ' get chart range
    FirstChartRange = shtData.Range("C4").Value2
    
    ' change data for first chart
    Set chart = shtDashboard.ChartObjects("FirstChart").chart
    With chart
        .HasTitle = True
        .ChartTitle.Text = FirstChartName
        On Error Resume Next
        .SetSourceData shtData.Range(FirstChartRange)
        On Error GoTo 0
        .Axes(xlValue).MaximumScaleIsAuto = True
    End With
       
End Sub

希望这对您的问题有所帮助!