Macro check if named option button physically exists and then do action accordingly?

Event2020

Board Regular
Joined
Jan 6, 2011
Messages
122
Office Version
  1. 2019
Platform
  1. Windows
I am using Excel 2007

I have a worksheet called Sheet1

On this worksheet I have two option buttons :
Code:
ActiveSheet.Shapes("OptionButton1")
ActiveSheet.Shapes("OptionButton2")

These two option buttons can be deleted by a macro depending on what a user does.

I need a macro that can do the following:
Check if ActiveSheet.Shapes("OptionButton1") and ActiveSheet.Shapes("OptionButton2") physically exists on Sheet1.

If they do not then
(continue with the code that follows eg. the rest of the macro)

If they do then
(run my delete code and then continue on)

There is a lots of examples via google on how to check if an object exists using Excel VBA but a lot of it seems very complicated which is fine but I would like something simple and easy to implement.

Something like:
Code:
ActiveSheet.Shapes("OptionButton1").exists then
(run my delete code and then continue on)

which I have tried and of course it does not work.;)

Many thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Something like

Code:
On Error Resume Next
ActiveSheet.Shapes("OptionButton1").Delete
ActiveSheet.Shapes("OptionButton2").Delete
On Error Goto 0

or
Code:
With ActiveSheet
    On Error Resume Next
    If .Shapes("OptionButton1").Name <> "OptionButton1" Then
        On Error Goto 0

        MsgBox "Button 1 does not exists"
    Else
        On Error Goto 0

        MsgBox "exists"
    End If
End With
 
Upvote 0
Something like

Code:
On Error Resume Next
ActiveSheet.Shapes("OptionButton1").Delete
ActiveSheet.Shapes("OptionButton2").Delete
On Error Goto 0

or
Code:
With ActiveSheet
    On Error Resume Next
    If .Shapes("OptionButton1").Name <> "OptionButton1" Then
        On Error Goto 0

        MsgBox "Button 1 does not exists"
    Else
        On Error Goto 0

        MsgBox "exists"
    End If
End With


Hi Mike

Thank you for taking the time to reply, much appreciated.

Your code does not quite do what I need.
It should be:

Macro actions
Check if "OptionButton1" & "OptionButton2" physically exist?
NO then
Do nothing and run the rest of the macro
Or
YES then
delete if "OptionButton1" & "OptionButton2" and run the rest of the macro
 
Upvote 0
The second bit of code shows two branches of whether OptionButton1 exists, put the code you want into the branch you want. Copying that code and changing the string to OptionButton2 and you can do the same for the other button.
 
Upvote 0
The second bit of code shows two branches of whether OptionButton1 exists, put the code you want into the branch you want. Copying that code and changing the string to OptionButton2 and you can do the same for the other button.


Hi Mike

You are a gentleman sir!

That works brilliantly.

Thank you.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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