How to define range with variable

tsx2004

New Member
Joined
Sep 20, 2006
Messages
3
Hi,

I'm trying to automate a chart plot, but I'm stuck on how to define a dynamic range.

ActiveChart.SetSourceData Source:=Sheets("ByFabLot%").Range("A4:A58, D4:D58"), _
PlotBy:=xlColumns

Instead of a static range, the A58 and D58 will be a variable and will be expanded.

I was trying to put in "A4: Xrow, D4: Yrow" as my variable, but i get an errow. Any suggestion? Thanks in advance
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Try this, assuming XRow is your variable.
Code:
 ActiveChart.SetSourceData Source:=Sheets("ByFabLot%").Range("A4:A" & XRow & ", D4:D" & XRow), _
PlotBy:=xlColumns
 
Upvote 0
The ampersands (&) are used in VBA to concatenate (join) strings.

You can also use them in worksheet formulas.

By the way this isn't the only way to acheive what you want.
 
Upvote 0
Thanks for the information.

What is another way? So, i can have another tool for future reference.
 
Upvote 0
Well you could try something like this, but I probably wouldn't recommend it.
Code:
Dim rng As Range

    With Sheets("ByFabLot%")
        Set rng = Union(.Range("A4").Resize(XRow - 3), .Range("D4").Resize(XRow - 3))
    End With
 
    ActiveChart.SetSourceData Source:=rng, PlotBy:=xlColumns
 
Upvote 0

Forum statistics

Threads
1,219,034
Messages
6,145,893
Members
450,654
Latest member
lorento

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top