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
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,831
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
75,831
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
75,831
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
 

Watch MrExcel Video

Forum statistics

Threads
1,109,161
Messages
5,527,156
Members
409,750
Latest member
BorisYeltsin

This Week's Hot Topics

Top