Extending a range horizontally for chart using VBA

mvervair

New Member
Joined
Aug 3, 2016
Messages
27
I'm doing a horizontal range but having issues. Here's my example code:
Code:
    resSurvey = Range("C3").End(xlToRight).Column
    Sheets("dash").ChartObjects("Chart 12").Activate
    ActiveChart.SetSourceData Source:=Range("res_surveys!$C$35:$" & resSurvey & "$35")


This doesn't work because my variable is a number. I'm trying to piece together how to use the Cells object inside the range object when the cells should refer to a different worksheet than the one my chart is on. Any advice, insight or helping me (and others) understand concepts I'm missing help!
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Hi

If I understood correctly, try:

Code:
Sub Test()
Dim cht As Chart
Dim resSurvey As Long
    
    Set cht = Sheets("dash").ChartObjects("Chart 12").Chart
    
    With Worksheets("res_surveys")
        resSurvey = .Range("C3").End(xlToRight).Column
        cht.SetSourceData Source:=.Range(.Cells(35, "C"), .Cells(35, resSurvey))
    End With

End Sub
 
Upvote 0
That's it, thank you kind stranger!

Out of curiosity, is there a meaningful difference in:
Code:
[COLOR=#333333]        cht.SetSourceData Source:=.Range(.Cells(35, "C"), .Cells(35, resSurvey))[/COLOR]
vs. not placing the "." before the Range
Code:
[COLOR=#333333]        cht.SetSourceData Source:=Range(.Cells(35, "C"), .Cells(35, resSurvey))[/COLOR]

I ask because the code seemed to work both ways whether it was .Range or Range.

Thank you again!
 
Upvote 0
Hi

I'm glad it helped.


About the Range() - .Range()

I qualify the Range object to make it clear that it is a range in the res_surveys worksheet (specified in the With block).

In this case you are right that both work because I have also qualified both the Cell()'s and so there is no doubt that we are referring to the res_surveys worksheet, but ...

that will not always be the case.

This is an example: let's say you want to specify the start range directly with the address like:

1 - Range qualified

Code:
        cht.SetSourceData Source:=.Range("C35", .Cells(35, resSurvey))

In this case it will work independently of which is the active sheet at the moment of execution. The .Range makes sure that we know that the "C35" refers to res_surveys.


2 - Range not qualified

Code:
        cht.SetSourceData Source:=Range("C35", .Cells(35, resSurvey))


In this case, however, sometimes it may work and sometime it won't.

Let's say that when you run the code the active worksheet is Sheet1 and the column number in the variable resSurvey is 5 (column "E").

The code sees "Range("C35", ..." and it will assume that you are talking about Sheet1!C35 because Sheet1 is the active worksheet. Then it sees "..., .Cells(35, resSurvey)" and, because Cell() is qualified it knows it refers to the worksheet res_surveys.

It's like you are specifying the range Sheet1!C35:res_surveys!E35 which makes no sense.

In this case the code will fail.

This code will only work if, at the moment of execution, the active worksheet is res_surveys.


So, in conclusion, you are right, in this case you may use both. I prefer, however, to always qualify the Range() making it clear that I'm working with the worksheet specified in the With block. It will work no matter how I specify the start and end range.
 
Upvote 0
Very clear, thanks again. This will allow me to clean up a lot of my charts' code now since I can qualify their data sources and stop jumping around between sheets.
 
Upvote 0

Forum statistics

Threads
1,214,920
Messages
6,122,279
Members
449,075
Latest member
staticfluids

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