Home » excel » excel – Show and Hide Shapes in Active Worksheet

excel – Show and Hide Shapes in Active Worksheet

Posted by: admin April 23, 2020 Leave a comment

Questions:
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Visible
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 3")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 3")).Visible
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 4")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 4")).Visible
ActiveSheet.Shapes.Range(Array("Rounded Rectangle 5")).Visible = Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle 5")).Visible

This is the sample of code macro that i use

i want to know how we can on run this code to hide only rectangle 1 line

like

For i = 1 To ActiveSheet.Shapes.Count
    ActiveSheet.Shapes.Range(Array("Rounded Rectangle *")).Visible = _ 
    Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle *")).Visible
Next i

I just dont know how to simplify correctly

How to&Answers:

Youre not far off , this is how to use the loop to go through all the Rounded Rectangles

Dim i As Long
For i = 1 To ActiveSheet.Shapes.Count
    ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible = _
    Not ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible
Next i

note how "Rounded Rectangle " & i is replaced with the
"Rounded Rectangle 1"
"Rounded Rectangle 2"
"Rounded Rectangle n" where n = i ( meaning the amount of times the loop runs )


therefore, if you want to hide only the first one Rounded Rectangle 1, then add an If/else statement to your loop

note: this code will never display the first Rounded Rectangle 1 and toggle between showing all the other ones. if you wanted to always show them then just assign true inside the else statement

Dim i As Long
For i = 1 To ActiveSheet.Shapes.Count
    If i = 1 Then
        ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible = False
    Else
        ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible = _
        ActiveSheet.Shapes.Range(Array("Rounded Rectangle " & i)).Visible
    End If
Next i