Identifying individual charts in VBA

kingofaces

Board Regular
Joined
Aug 23, 2010
Messages
68
I have a macro I wrote to modify the the axes of a single selected chart (Chartaxis). There are 16 charts in total in this page, and I would like to be able to run a macro that will update the axes of all charts in one go. I tried using the recorder to select the chart and run the Chartaxis macro, and then select the next chart, run macro, etc. Now when I go back and try to run this new macro (Updateallcharts) nothing happens when the charts should be updating. Here's a small snippet of what the code looks like when I tried it with just 4 charts.

Code:
Sub Updateallcharts()

    ActiveSheet.ChartObjects("Chart 15").Activate
    Application.Run "newscptemplate.xlsm!Chartaxis"
    ActiveSheet.ChartObjects("Chart 15").Activate
    Application.Run "newscptemplate.xlsm!Chartaxis"
    ActiveSheet.ChartObjects("Chart 15").Activate
    Application.Run "newscptemplate.xlsm!Chartaxis"
    ActiveSheet.ChartObjects("Chart 16").Activate
    Application.Run "newscptemplate.xlsm!Chartaxis"
End Sub

This first thing I notice is that three different charts are named Chart 15, so is that causing any issues? Again, if I manually click the chart and run the Chartaxis macro things work just fine. However if I reduce the Updatedallcharts macro to just one chart nothing updates:

Code:
Sub Updateallcharts()

    ActiveSheet.ChartObjects("Chart 15").Activate
    Application.Run "newscptemplate.xlsm!Chartaxis"
    
End Sub

It's odd considering this last snippet should be exactly the same as manually clicking one chart and running the Chartaxis macro in practice. Is there some quirk in how VBA interacts with charts that I'm not catching here?

In case it's needed, here's the chartaxis macro:

Code:
Sub Chartaxis()

     ActiveChart.Axes(xlCategory).Select
     ActiveChart.Axes(xlValue).MinimumScale = -40
     ActiveChart.Axes(xlValue).MaximumScale = 0
    With ActiveChart.Axes(xlCategory)
        .MinimumScale = 0
        .MaximumScale = 6000
        .MinorUnit = 500
        .MajorUnit = 500
        .Crosses = xlAutomatic
        .ReversePlotOrder = False
        .ScaleType = xlLinear
        .DisplayUnit = xlNone
    End With
End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Maybe easier to identify chart objects by their index number:
Code:
Sub UpdateChrts()
Dim n As Long, i As Long

n = ActiveSheet.ChartObjects.Count
With ActiveSheet
    For i = 1 To n
        .ChartObjects(i).Select
        Call Chartaxis
    Next i
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,383
Messages
6,119,198
Members
448,874
Latest member
Lancelots

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