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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
Try this, assuming XRow is your variable.
Code:
 ActiveChart.SetSourceData Source:=Sheets("ByFabLot%").Range("A4:A" & XRow & ", D4:D" & XRow), _
PlotBy:=xlColumns
 

tsx2004

New Member
Joined
Sep 20, 2006
Messages
3
It works well. Thanks a lot. Do you mind to tell me what are the & for? Thanks
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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.
 

tsx2004

New Member
Joined
Sep 20, 2006
Messages
3
Thanks for the information.

What is another way? So, i can have another tool for future reference.
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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
 

Forum statistics

Threads
1,141,139
Messages
5,704,508
Members
421,353
Latest member
jekoxien15

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
Top