简体   繁体   English

动态图表 VBA 的范围为空

[英]Range Empty for Dynamic Chart VBA

I'm trying to create a dynamic sourcedata on a chart in VBA as this can vary depending on the data being pulled in via a macro.我正在尝试在 VBA 中的图表上创建动态源数据,因为这可能会因通过宏引入的数据而异。 So far I have:到目前为止,我有:

Set Rng = Sheets("Mapping Tables").Range("J13", Sheets("Mapping Tables").Range("J13").End(xlDown).End(xlToRight)).Select

    Worksheets("Vintage").Charts("Vintage_1").SetSourceData Rng, PlotBy:=xlColumns

However, when I step through the code in the set range command, my cells are being selected.但是,当我逐步执行 set range 命令中的代码时,我的单元格被选中。 When I run through the SetSourceData step nothing happens.当我运行 SetSourceData 步骤时,没有任何反应。 When I hover over the Rng variable it says = nothing.当我将鼠标悬停在 Rng 变量上时,它说 = 无。

I've never really done dynamic charts before but I cannot understand why my range equals nothing when my range is being selected.我以前从未真正做过动态图表,但我不明白为什么在选择我的范围时我的范围等于零。

Thanks in advance.提前致谢。

actually the first line should give you an error, if I'm not mistaken.实际上第一行应该给你一个错误,如果我没记错的话。 You cannot set a range to a ".select" statement.您不能为“.select”语句设置范围。 Try deleting the ".select" at the end尝试删除最后的“.select”

Edit: when you're defining a continuous range (eg a table), you can use something cleaner:编辑:当您定义连续范围(例如表格)时,您可以使用更清洁的东西:

set Rng = Sheets("Mapping Tables").Range("J13").CurrentRegion

Try this.尝试这个。

Sub test()
    Dim Rng As Range
    Dim obj As ChartObject
    Set Rng = Sheets("Mapping Tables").Range("J13", Sheets("Mapping Tables").Range("J13").End(xlDown).End(xlToRight))

    Set obj = Worksheets("Vintage").ChartObjects("Vintage_1")
    With obj.Chart
        .SetSourceData Rng, PlotBy:=xlColumns
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM