macro to change worksheet reference in charts

maximus315

New Member
Joined
Aug 2, 2011
Messages
7
I am trying to write a macro that changes just the worksheet name in a chart that is already set up. <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Situation:<o:p></o:p>
I have a worksheet setup with multiple charts. All the charts reference ranges in a template worksheet. A new worksheet is created from the template for each new batch loaded in “YTDDATA” worksheet. The ranges in the new worksheets will all be the same so I just want to change the referenced worksheet name in the charts to update to the latest batch. All the charts have different ranges. I have <o:p></o:p>
<o:p> </o:p>
Step one -- to locate the last batch added in YTDDATA – this works<o:p></o:p>
Step two – open the worksheet with the charts “Operator Trends” – this works <o:p></o:p>
Step three -- change worksheet name referenced in charts -- this is where I am having trouble.
<o:p> </o:p>
Help would be greatly appreciated. <o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
code<o:p></o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
<o:p> </o:p>
Sub Chartupdatenew()<o:p></o:p>
'<o:p></o:p>
'<o:p></o:p>
Dim iCurrentRow As Integer<o:p></o:p>
Dim i As Integer<o:p></o:p>
Dim c As Integer<o:p></o:p>
Dim sSheetName As String<o:p></o:p>
<o:p></o:p>
'Get last row with a batch number<o:p></o:p>
Sheets("YTDDATA").Select<o:p></o:p>
<o:p></o:p>
Dim bEmptyCellFound As Boolean<o:p></o:p>
<o:p></o:p>
'set Variables<o:p></o:p>
i = 7 'start at row 7<o:p></o:p>
bEmptyCellFound = False<o:p></o:p>
<o:p></o:p>
Do Until bEmptyCellFound = True<o:p></o:p>
If (Sheets("YTDDATA").Cells(i, 8).Value < 1) Then<o:p></o:p>
iCurrentRow = i 'Row information for later use<o:p></o:p>
bEmptyCellFound = True ' set loop exit flag<o:p></o:p>
c = i - 1<o:p></o:p>
End If<o:p></o:p>
i = i + 1 'step to next row<o:p></o:p>
<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><st1:place w:st="on">Loop</st1:place><o:p></o:p>
<o:p></o:p>
'set sSheetName to batch worksheet name<o:p></o:p>
<o:p> </o:p>
sSheetName = Sheets("YTDDATA").Cells(c, 8).Value<o:p></o:p>
<o:p></o:p>
'set worksheet to chart worksheet<o:p></o:p>
<o:p></o:p>
Sheets("Operator Trends").Select<o:p></o:p>
Sheets("Operator Trends").Activate<o:p></o:p>

'change worksheet name referenced in charts<o:p></o:p>
<o:p> </o:p>
ActiveSheet.ChartObjects("Chart 4").Activate<o:p></o:p>
ActiveChart.SeriesCollection(1).XValues = "=ssheetname!R72C1:R113C1"<o:p></o:p>
ActiveChart.SeriesCollection(1).Values = "=sSheetname!R72C57:R113C57"<o:p></o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
never mind i figured it out
just needed to change the reference code a little

sSheetName = "'" & Sheets("YTDDATA").Cells(c, 8).Value
and then change
ActiveChart.SeriesCollection(1).XValues = "=" & sSheetName & "'!R72C1:R113C1"
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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