Refering to the active worksheet in a macro

RAGTIME333

New Member
Joined
Jun 23, 2009
Messages
2
Hi I am new to VB and recorded a macro to update a number of graphs in a number of worksheets with additional data. This is what i recorded

Sub EDITCHART2()
'
' EDITCHART2 Macro
' Macro recorded 6/23/2009 by mcarag
'
' Keyboard Shortcut: Ctrl+Shift+W
'

ActiveChart.PlotArea.Select
ActiveChart.SeriesCollection(1).XValues = "=(Q11.5!R11C2:R15C2,Q11.5!R17C2)"
ActiveChart.SeriesCollection(1).Values = "=(Q11.5!R11C3:R15C3,Q11.5!R17C3)"
ActiveChart.SeriesCollection(2).XValues = "=(Q11.5!R11C2:R15C2,Q11.5!R17C2)"
ActiveChart.SeriesCollection(2).Values = "=(Q11.5!R11C4:R15C4,Q11.5!R17C4)"
ActiveChart.SeriesCollection(3).XValues = "=(Q11.5!R11C2:R15C2,Q11.5!R17C2)"
ActiveChart.SeriesCollection(3).Values = "=(Q11.5!R11C5:R15C5,Q11.5!R17C5)"
End Sub

However I want the macro to reference each worksheet I run it in, rather than Q11.5 each time. Simple I'm sure and let here I sit...
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
NOT TESTED
Code:
MySheet = Activesheet.name
ActiveChart.SeriesCollection(1).XValues = "=(" & MySheet & "!R11C2:R15C2," & MySheet &"!R17C2)"
 
Upvote 0
I feel like you are on the right track with the & symbols but it don't work it gives me an unexpected end of statement error
 
Upvote 0
Perhaps couild you add an extra variable
Code:
AAA = "=(" & MySheet & "!R11C2:R15C2," & MySheet &"!R17C2)"<!-- / message -->
and check if the value is correct, means it looks like the previous value
"=(Q11.5!R11C2:R15C2,Q11.5!R17C2)"
Does variable MySheet has the right value.
 
Upvote 0
MySheet=ActiveSheet.name? That assigns a string value (the name of the Active Sheet, eg, "Sheet1") to MySheet. If you want MySheet to represent a sheet object, you need to use:

Code:
Dim MySheet as Worksheet
Set MySheet=ActiveSheet

However, I don't know why you need to do that - just use ActiveSheet.Range(range spec) - this will refer the referenced range on the active sheet (that is, the sheet where the currently selected cell resides).

For example, you could use
Code:
ActiveChart.SeriesCollection(1).XValues = ActiveSheet.Range("Q11.5!R11C2:R15C2,Q11.5!R17C2")
although I'm not sure I understand the syntax of that range specification (I don't do a lot of charting from VBA).

HTH,

EricF
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,999
Members
452,373
Latest member
TimReeks

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