图表中的VBA循环更新使用来自另一个工作表的数据

时间:2017-07-12 15:24:16

标签: excel vba excel-vba loops excel-charts

在工作表(“费用”)中,以下代码中注明了错误。一旦错误得到解决,我计划将'c(x)'计数器循环遍历每个图表

运行时错误'1004': 对象'_Worksheet'的方法'Range'失败

我认为该错误与我引用其他工作表的方式有关

Dim p As Worksheet, cost As Worksheet
Set cost = Workbooks("dashboard.xlsm").Worksheets("Cost")
Set p = Workbooks("dashboard.xlsm").Worksheets("p")

Dim c(1 To 3) As Variant

p.Activate
'[c(x)] denotes ranges for each chart object
'modified by counter [i], denoting ranges for each series
Set c(1) = p.Range(Cells(3, 1 + i), Cells(3, 1 + i).End(xlDown))
Set c(2) = p.Range(Cells(3, 4 + i), Cells(3, 4 + i).End(xlDown))
Set c(3) = p.Range(Cells(3, 8 + i), Cells(3, 8 + i).End(xlDown))

cost.Activate
For i = 1 To 2
    With ActiveSheet.ChartObjects("Chart 1").Chart.SeriesCollection(i)
    'all charts follow same time series
    .XValues = p.Range(Range("a3"), Range("a3").End(xlDown)) '**error here**
    .Values = c(1)
    End With
Next i

1 个答案:

答案 0 :(得分:0)

尝试更换您的专线:

.XValues = p.Range(Range("a3"), Range("a3").End(xlDown))

使用:

.XValues = "=" & p.Range(p.Range("a3"), p.Range("a3").End(xlDown)).Address(False, False, xlA1, xlExternal)