VBA Toggle button to remove shapes on worksheet.

Certified

Board Regular
Joined
Jan 24, 2012
Messages
189
Hello.

I have a number of shapes on my worksheet (text boxes, arrows and so on). I am trying to create a button that when pressed will hide (visible =false) the shapes on the worksheet. And when pressed again will show the shapes again.

Can someone help me with that?

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
See how this works,

It hides or unhides all shapes
Code:
    If ActiveSheet.DrawingObjects.Visible = True Then
        ActiveSheet.DrawingObjects.Visible = False
    Else: ActiveSheet.DrawingObjects.Visible = True
    End If
 
Upvote 0
The only part I was able to figure out is how to set the visibility to false.

Code:
ActiveSheet.Shapes("").Visible = False

So basically i want to code to look like this.

Button is selected ON

Code:
ActiveSheet.Shapes("").Visible = False

Button is selected OFF

Code:
ActiveSheet.Shapes("").Visible = True
 
Upvote 0
The syntax for the button will vary depending on what type of button you used. The example below uses a Forms control button. If you used an Active-X button then the name would be CommandButton#, with the # representing whatever index number it is. This allows you to hide all shapes, except the button that runs the macro.
Code:
Sub delShps()
Dim sh As Worksheet
Set sh = ActiveSheet
    For Each shp In sh.Shapes
        If shp.Name <> "Button 1" Then
            shp.Visible = False
        End If
    Next
End Sub
To make them visible again, change the False to True.
 
Upvote 0
Hi,

Thanks, but I wanted to be able to toggle between visible and not visible to pressing the button.
 
Upvote 0
Hi,

Thanks, but I wanted to be able to toggle between visible and not visible to pressing the button.

The problem is that the button itself is a shape, so if you use something like.
Code:
Sub shps()
If ActiveSheet.Shapes.Visible = True Then
	ActiveSheet.Shapes.Visible = False
Else 
	ActiveSheet.Shapes.Visible = True
End If
Which is the normal toggle syntax, then you will hide your button also. So that won't work. You almost certainly have to use a loop to do what you want. I am not sure what criteria could be used to do what you want.
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top