Home > other >  Hide/unhide multiple shapes Excel VBA
Hide/unhide multiple shapes Excel VBA

Time:06-30

I am very new to excel VBA & macros and I am trying to do something that I think is very simple, but I cannot for the life of me figure it out.

I have a shape ("shape 1") that when clicked should show/unhide two shapes ("shape 2" and "shape 3").

By default "shape 2" and "shape 3" should be hidden and only appear when "shape 1" is selected.

Any help will be much appreciated, remembering I am a complete novice here!

Edit:

I have managed to use the below, essentially a copy paste from here, I dont know what it means but it works for the single button. I cant figure out how to extend the code to include multiple objects being shown/hidden. An example of a second object to be shown/hidden at the same time as "july_2022" is "august_2022".

Public HIDE As Boolean

Sub fy ()
ActiveSheet.Shapes("july_2022").Visible = HIDE
If ActiveSheet.Shapes("july_2022").Visible = False Then
HIDE = True
Else
HIDE = False
End If
End Sub

CodePudding user response:

ActiveSheet.Shapes("july_2022").Visible = HIDE is the part that sets the visibility of a shape (july_2022). Another identical line but with something other than july_2022 would affect a second shape. The rest of the code (If.. Then.. Else.. End If) could be replaced with HIDE=Not(HIDE).

For example, the following code when run will 'toggle' the visibility of two shapes on the Active Sheet called 'Shape2' and 'Shape3'.

Public HIDE As Boolean

Sub fy()
    ActiveSheet.Shapes("Shape2").Visible = HIDE
    ActiveSheet.Shapes("Shape3").Visible = HIDE
    HIDE = Not (HIDE)
End Sub

CodePudding user response:

The Post by CLR really helped but I want to add in an additional criteria that if one is 'toggled', anything that has been unhidden by the other is hidden.

I adapted the code provided to use groups, to save on having to enter so many shapes individually. Below is code for three different, toggle buttons, which all work, but

Sub toggle_fy_records() UNHIDES "group_fy_2021_22" and "group_fy_2022_23", which allows for them to be selected via similar code to display additional objects.

My current codes allocated to respective shapes in the sheet are:

Sub toggle_fy_records()
    With ActiveSheet.Shapes("fy_records")
        If .Visible = False Then .Visible = True Else .Visible = False
    End With
End Sub

Sub HideShow_fy_months_2021_2022()
    With ActiveSheet.Shapes("group_fy_2021_22")
        If .Visible = False Then .Visible = True Else .Visible = False
    End With
End Sub

Sub HideShow_fy_months_2022_2023()
    With ActiveSheet.Shapes("group_fy_2022_23")
        If .Visible = False Then .Visible = True Else .Visible = False
    End With
End Sub

The below code is the one I want that if selected, will hide all other objects other then that which is specified.

Sub HideShow_account_info()
    With ActiveSheet.Shapes("account_codes")
        If .Visible = False Then .Visible = True Else .Visible = False
    End With
End Sub
  • Related