Why does the VBA code only changes the chartobject on the activesheet, code runs fine

Diag

New Member
Joined
Aug 24, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am working on a piece of code that changes the y-value(min-max) on the axis and fills the colour in the graph.

Each worksheet has the same chart "Chart 1" with different data. With the code i want to "loop" through all the worksheets and change the Y axis value and colour the graph.

VBA Code:
Private Sub CommandButton4_Click()

 Dim ws             As Worksheet
 Dim wb             As Workbook
 Dim ch             As Chart
 Dim cht            As ChartObject
 Dim yax            As Axis
 
 Set ch = ActiveSheet.ChartObjects("Chart 1").Chart
  
    For Each ws In ActiveWorkbook.Sheets
        ws.Range("E3:L38,O5:O6,O10:O38,O1,E1,B1,C3:C38,B36:D38,B6:D8").ClearContents
           
        For Each cht In ActiveSheet.ChartObjects
        ActiveSheet.ChartObjects("Chart 1").Activate
        
            With cht.Chart
            With .Axes(xlValue)
                .MaximumScale = 100
                .MinimumScale = 0
            End With
            End With
        Next cht

  'Change first bar chart series fill color
  ch.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(50, 74, 168)
  
    Next
    
    MsgBox "Check if modifications are performed accordingly!!"
    
    Noise.Hide
    
End Sub

For the active sheet the code runs perfectly, The "loop" clearcontents works fine through all sheets.

However, the part of code that changes the axis value and colour doesnt only work on the active sheet. all other sheets remain unchaged.

The code doesn't give any error.

hope someone can assist.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Your loop code explicitly refers to the active sheet. Replace this:

VBA Code:
       For Each cht In ActiveSheet.ChartObjects
        ActiveSheet.ChartObjects("Chart 1").Activate

with this:

VBA Code:
       For Each cht In ws.ChartObjects
 
Upvote 0
Hello Rory

thanks for your reply.

The first problem is solved. All Yaxis are changed.

Now the onlything is the coloutfill of the graph.

the colourfill still doesnt work on all sheets.

thanks
 
Upvote 0
You need:

Code:
 cht.Chart.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(50, 74, 168)

and that line should be inside your For Each loop, not after it.
 
Upvote 0
Hello Rory,

I have tried to place it inside the "for" loop and tried several things but only the active sheet graph is coloured.

the code doesnt respond to the othe graphs.



VBA Code:
Private Sub CommandButton4_Click()

 Dim ws             As Worksheet
 Dim wb             As Workbook
 Dim ch             As Chart
 Dim cht            As ChartObject
 Dim yax            As Axis
 
 Set ch = ActiveSheet.ChartObjects("Chart 1").Chart

    
    For Each ws In ActiveWorkbook.Sheets
       
        ws.Range("E3:L38,O5:O6,O10:O38,O1,E1,B1,C3:C38,B36:D38,B6:D8").ClearContents
    
        
        For Each cht In ws.ChartObjects
        
With cht.Chart
        ActiveSheet.ChartObjects("Chart 1").Activate
        ch.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(50, 74, 168)
 End With
        
   With cht.Chart
            With .Axes(xlValue)
                .MaximumScale = 100
                .MinimumScale = 0
                
   
     End With
 End With
            
          
 
Next cht


Next
    
    MsgBox "Check if modifications are performed accordingly!!"
    
    Noise.Hide
    
End Sub
 
Upvote 0
You didn't change the code as I indicated. This:

Code:
 ch.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(50, 74, 168)

needs to be this:

Rich (BB code):
 cht.Chart.SeriesCollection(1).Format.Fill.ForeColor.RGB = RGB(50, 74, 168)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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