What should I add to the macro so that the macro only run on the selected chart

  • Hi all experts,


    I'm writing a macro by resizing the chart, and the VBA codes as below.
    Dunno why when I run the marco, it changed all the charts size in the same spreadsheet, just wondering what should I add to the code, so that the macro is only run on the selected chart?
    Really appreciate if you guys can help me with this. And thank you so much!



    For j = 1 To ActiveSheet.Shapes.Count
    If ActiveSheet.Shapes(j).Type = msoChart Then
    ActiveSheet.Shapes(j).Width = 360
    ActiveSheet.Shapes(j).Height = 201.6
    End If
    Next j
    End Sub

  • Re: What should I add to the macro so that the macro only run on the selected chart


    Welcome to the forum.


    It's running on all charts because you told it to with that loop. To do just the active one, use:


    Code
    1. With ActiveChart.Parent
    2. .Width = 360
    3. .Height = 201.6
    4. End With
    5. End Sub

    Rory
    Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: nothing works and they don’t know why

  • Re: What should I add to the macro so that the macro only run on the selected chart


    Thank you so much for your help, is working now.


    I'm new to the forum, also new to the VBA code, still try to figure alot of the VBA code :(