Using a Macro to update a chart range

Benno64

New Member
Joined
Aug 24, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello Everyone,

I am new to Macro's and have been googling/youtub'ing a lot so far, but I am now stuck. I have a spreadsheet with 20 sheets in that are all identical and house data within a table (all of which are the same amount of rows). The data from each sheet feeds into a graph that is on the same sheet. I have noticed that there is an error in the graph and it is not selecting all the rows in the table. I wrote a macro using the macro recorded and recorded the actions on sheet 1. The steps I recorded were to change the graph range to include all rows in the table and also a few other chat formatting things.

I applied the macro to the other sheets and I thought it worked fine. However the graphs on sheets 2-20 have now all been updated so that the ranges pull from sheet 1 and not the sheets that the graphs are located on. I dont know if im missing something really simple or a basic understanding but I cant for the life of me figure it out. I just want the macro to run within the sheet and not use the range from sheet 1. Here is the VBA code below.

In this example 'sheet 1' that I have mentioned above is "Blend 2.1 (intra-gran)"

Hoping anyone can help! Thanks

Sub newRETAINED()
'
' newRETAINED Macro
'

'
Columns("H:Z").Select
Selection.EntireColumn.Hidden = False
ActiveWindow.SmallScroll Down:=54
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.FullSeriesCollection(1).XValues = _
"='Blend 2.1 (intra-gran)'!$B$22:$B$30"
ActiveChart.FullSeriesCollection(1).Values = _
"='Blend 2.1 (intra-gran)'!$F$22:$F$30"
ActiveChart.FullSeriesCollection(2).Name = "='Blend 2.1 (intra-gran)'!$M$48"
ActiveChart.FullSeriesCollection(3).Name = "='Blend 2.1 (intra-gran)'!$M$49"
ActiveChart.FullSeriesCollection(4).Name = "='Blend 2.1 (intra-gran)'!$M$50"
ActiveWindow.SmallScroll Down:=39
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.FullSeriesCollection(3).DataLabels.Select
Selection.ShowSeriesName = True
Selection.ShowRange = False
ActiveChart.FullSeriesCollection(2).DataLabels.Select
Selection.ShowSeriesName = True
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Untested, therefore please test on a copy of your workbook.

VBA Code:
Sub newRETAINED()
'
' newRETAINED Macro
'

'
Columns("H:Z").Select
Selection.EntireColumn.Hidden = False
ActiveWindow.SmallScroll Down:=54
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.FullSeriesCollection(1).XValues = _
"='" & ActiveSheet.Name & "'!$B$22:$B$30"
ActiveChart.FullSeriesCollection(1).Values = _
"='" & ActiveSheet.Name & "'!$F$22:$F$30"
ActiveChart.FullSeriesCollection(2).Name = "='" & ActiveSheet.Name & "'!$M$48"
ActiveChart.FullSeriesCollection(3).Name = "='" & ActiveSheet.Name & "'!$M$49"
ActiveChart.FullSeriesCollection(4).Name = "='" & ActiveSheet.Name & "'!$M$50"
ActiveWindow.SmallScroll Down:=39
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.FullSeriesCollection(3).DataLabels.Select
Selection.ShowSeriesName = True
Selection.ShowRange = False
ActiveChart.FullSeriesCollection(2).DataLabels.Select
Selection.ShowSeriesName = True
End Sub
The macro could be refined by directly referring to the chart on a sheet instead of the Activate and ActiveSheet code that the macro recorder gives you. Also, to loop through all the sheets rather than having to manually activate each sheet.
 
Upvote 0
Untested, therefore please test on a copy of your workbook.

VBA Code:
Sub newRETAINED()
'
' newRETAINED Macro
'

'
Columns("H:Z").Select
Selection.EntireColumn.Hidden = False
ActiveWindow.SmallScroll Down:=54
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.FullSeriesCollection(1).XValues = _
"='" & ActiveSheet.Name & "'!$B$22:$B$30"
ActiveChart.FullSeriesCollection(1).Values = _
"='" & ActiveSheet.Name & "'!$F$22:$F$30"
ActiveChart.FullSeriesCollection(2).Name = "='" & ActiveSheet.Name & "'!$M$48"
ActiveChart.FullSeriesCollection(3).Name = "='" & ActiveSheet.Name & "'!$M$49"
ActiveChart.FullSeriesCollection(4).Name = "='" & ActiveSheet.Name & "'!$M$50"
ActiveWindow.SmallScroll Down:=39
ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.FullSeriesCollection(3).DataLabels.Select
Selection.ShowSeriesName = True
Selection.ShowRange = False
ActiveChart.FullSeriesCollection(2).DataLabels.Select
Selection.ShowSeriesName = True
End Sub
The macro could be refined by directly referring to the chart on a sheet instead of the Activate and ActiveSheet code that the macro recorder gives you. Also, to loop through all the sheets rather than having to manually activate each sheet.
Hey, this worked great! Sorry for a slight delay in responding here - I've been out of office in work. Im going to cycle it through all sheets now :) Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

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