Search code examples
excelvbaexcel-2019

Hide one group while the other is visible vba


I have one group of objects "Group 34" and another group "Group 6". I want one group to be visible while the other is hidden. I thought it would be easier to make two buttons instead of one toggleable one as i didnt know how to so if any of you could help with that it would be great but, I faced an error with the two buttons option. This is my code for two buttons:

Private Sub CommandButton3_Click()

ActiveSheet.Shapes("Group 34").Visible = False
ActiveSheet.Shapes("Group 6").Visible

End Sub

Private Sub CommandButton4_Click()

ActiveSheet.Shapes("Group 34").Visible
ActiveSheet.Shapes("Group 6").Visible = False

End Sub

The error is in the third line of the code for each where it says "Runtime Error 438. Object doesn't support this property or method"


Solution

  • Like this:

    Sub Tester()
    
        Dim vis
        With ActiveSheet
            vis = .Shapes("Group 34").Visible
            .Shapes("Group 34").Visible = Not vis 'Toggle visibility
            .Shapes("Group 6").Visible = vis
        End With
    
    End Sub