Search code examples
vbapowerpoint

Toggle Axis on/off PowerPoint VBA


How do i combine these two to make the macro a toggle instead of needing two different macros to turn the axis on and off?

I can't seem to get it to work with a If else statement.

Sub Format_linechart_AxisTrue()

    Dim sld As slide
    Dim shp As shape
    Dim chart As chart
    Dim sr As series
    Dim i As Long
    
    Set sld = Application.ActiveWindow.View.slide
    
    For Each shp In sld.Shapes
        If shp.HasChart Then
            Set chart = shp.chart
            For i = 1 To chart.SeriesCollection.Count
                If chart.HasAxis(xlValue) = False Then chart.HasAxis(xlValue) = True
            Next i
        End If
    Next shp

End Sub

Sub Format_linechart_AxisFalse()

    Dim sld As slide
    Dim shp As shape
    Dim chart As chart
    Dim sr As series
    Dim i As Long
    
    Set sld = Application.ActiveWindow.View.slide
    
    For Each shp In sld.Shapes
        If shp.HasChart Then
            Set chart = shp.chart
            For i = 1 To chart.SeriesCollection.Count
                If chart.HasAxis(xlValue) = True Then chart.HasAxis(xlValue) = False
            Next i
        End If
    Next shp

End Sub

Solution

  • If you need a single macro able with deal with both situations running consecutively you should replace this part:

        For i = 1 To chart.SeriesCollection.Count
             If chart.HasAxis(xlValue) = True Then chart.HasAxis(xlValue) = False
        Next i
    

    with this adapted code:

        If chart.HasAxis(xlValue) = True Then 
           chart.HasAxis(xlValue) = False
        Else
           chart.HasAxis(xlValue) = True
        End if
    

    Now, at the second run it does the opposite...

    Iterating between the series (useless) will acct according to the series number (even or not...)