组合SetSourceData范围

时间:2018-03-19 08:53:51

标签: vba excel-vba excel

请帮助解决我的对象所需错误

Dim MyRange As Range

With Sheets("DATA")

 Set ChartRange251 = .Range(.Cells(NumFlds2, "P"), .Cells(NumFlds, "P"))
 ChartRange251Addr = ChartRange251.Address(External:=True)

 Set ChartRange252 = .Range(.Cells(NumFlds2, "AB"), .Cells(NumFlds,"AB"))
 ChartRange252Addr = ChartRange252.Address(External:=True)

 Set ChartRange253 = .Range(.Cells(NumFlds2, "AC"), .Cells(NumFlds, "AC"))
 ChartRange253Addr = ChartRange253.Address(External:=True)

 Set ChartRange254 = .Range(.Cells(NumFlds2, "AD"), .Cells(NumFlds, "AD"))
 ChartRange254Addr = ChartRange254.Address(External:=True)

 Set ChartRange255 = .Range(.Cells(NumFlds2, "AE"), .Cells(NumFlds, "AE"))
 ChartRange255Addr = ChartRange255.Address(External:=True)

 Set ChartRange256 = .Range(.Cells(NumFlds2, "AF"), .Cells(NumFlds, "AF"))
 ChartRange256Addr = ChartRange256.Address(External:=True)

End With

Sheets("DrillDown").ChartObjects("Chart 25").Activate
ActiveChart.ChartArea.Select
ActiveChart.SetSourceData Union(ChartRange251Addr, ChartRange252Addr, ChartRange253Addr, ChartRange254Addr, ChartRange255Addr, ChartRange256Addr)

错误发生在最后一行

ActiveChart.SetSourceData Union(ChartRange251Addr, ChartRange252Addr, ChartRange253Addr, ChartRange254Addr, ChartRange255Addr, ChartRange256Addr)

由于

1 个答案:

答案 0 :(得分:1)

使用Application.Union需要Range,而不是MSDN Union中的String

此外,您在代码中使用With语句非常好,为什么要用

破坏它
Sheets("DrillDown").ChartObjects("Chart 25").Activate
ActiveChart.ChartArea.Select

并且

ActiveChart.SetSourceData

而不是使用ActivateSelect完全符合您的ChartObject

修改后的代码

Dim ChtObj As ChartObject

With Sheets("DATA")
    Set ChartRange251 = .Range(.Cells(NumFlds2, "P"), .Cells(NumFlds, "P"))

    Set ChartRange252 = .Range(.Cells(NumFlds2, "AB"), .Cells(NumFlds, "AB"))

    Set ChartRange253 = .Range(.Cells(NumFlds2, "AC"), .Cells(NumFlds, "AC"))

    Set ChartRange254 = .Range(.Cells(NumFlds2, "AD"), .Cells(NumFlds, "AD"))

    Set ChartRange255 = .Range(.Cells(NumFlds2, "AE"), .Cells(NumFlds, "AE"))

    Set ChartRange256 = .Range(.Cells(NumFlds2, "AF"), .Cells(NumFlds, "AF"))
End With

' set the chart object range
Set ChtObj = Sheets("DrillDown").ChartObjects("Chart 25")

With ChtObj
    .Chart.SetSourceData Union(ChartRange251, ChartRange252, ChartRange253, ChartRange254, ChartRange255, ChartRange256)
End With