简体   繁体   中英

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. 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. When I run through the SetSourceData step nothing happens. When I hover over the Rng variable it says = nothing.

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. Try deleting the ".select" at the end

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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