Macro to remove previous and add current variable dynamic series in a chart

Peter Davison

Active Member
Joined
Jun 4, 2020
Messages
435
Office Version
  1. 365
Platform
  1. Windows
I have sent this query in before but I'm not sure I have explained it very well.
I have a clustered stacked column chart but the length of the Y series is variable and can be hundreds (X series is static)
I want to see if there is a macro that can remove all the existing series and add the new series however long it is every time I need to change the chart.
My Y series values starts in Cell B7:AH7 with the Series Name in B7 (always the starting Cell).
I think I need the variable to do an end and end up on the row B to see the end row the number of series' needs to go to.

My Chart is called Chart 8 and is in the sheet called Cluster Graph

Can anyone help me with a macro that can do the above?
Your help is really appreciate.
One of the your team did help me with a renaming of Legends in a chart which was really successful.

As always thank you for your time.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
VBA Code:
Sub Remake_Chart_Sheet1()
     '***************************************************************************
     'the chart, you have in sheet1, delete all existing series and add the actual "in use" series of sheet1
     '***************************************************************************
     Dim cX    As Range

     Set sh = Sheets("sheet1") 'this sheet

     Set chrt = sh.ChartObjects(1).Chart '1st chart in that sheet (there is only 1 for the moment)
     With chrt
          
          For i = .FullSeriesCollection.Count To 1 Step -1
               .FullSeriesCollection(i).Delete 'delete all existing series
          Next

          Set cX = sh.Range("C4:V6") 'the values for the X-axis (thus also the number of columns)
          
          For Each c In sh.Range("B7:B205").Cells 'loop through this cells
               If Len(c.Value) > 0 And c.Value <> 0 Then 'if they have a non-zero value then it's a new serie
                    With .SeriesCollection.NewSeries
                         .Name = "='" & sh.Name & "'!" & c.Address 'the name
                         .XValues = "='" & sh.Name & "'!" & cX.Address 'x-range
                         .Values = "='" & sh.Name & "'!" & c.Offset(, 1).Resize(, cX.Columns.Count).Address 'values
                    End With
               End If
          Next
  
     End With
     
     DoEvents
     MsgBox "still with a mixture of colors"
        
     With_New_Colors chrt 'give the series a user defined color
     
End Sub
 
Upvote 0
Solution
As communicated -
Thank you for all the help, I think I have most working.
Is it possible in the code above to just make it have prime clear colours as the default then there will be little need to probably change colours.
Just colours that are easy to read the data labels
and also switch data labels on automatically?

There were a couple of changes to the above code -
Sheet1 will be Cluster Graph
X Axis Range will be C4:AH6
Y Axis Range will be B7:B1500

Appreciate your help
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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