PDA

View Full Version : Solved: Delete Menu if exists



justdriving
09-17-2011, 03:02 PM
I want to delete a menu if it exists.
I wanted to correct this VBA.



For i = 1 to NumberofControls

If Control.name = "Menu" Then
Application.CommandBars("Worksheet Menu Bar").Controls(i).Delete
End If

Next i

Bob Phillips
09-17-2011, 03:18 PM
On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("Menu").Delete
On Error Goto 0

justdriving
09-17-2011, 03:20 PM
Thanks, but, if Controls("Menu") did not exist earlier then VBA throws an error. Therefore, I want to detect, if it exists earlier. Is there any way possible to detect?

Aussiebear
09-17-2011, 04:55 PM
Set up an If test, so that if it exists then a msgbox indicates it exists else the msgbox suggests it does not exist

justdriving
09-17-2011, 05:11 PM
Oh ok ... Anyway Bob's answer worked well. I had set up Error to go to certain procedure. I will have to move this step afterwards. Thanks to all of you.

Bob Phillips
09-18-2011, 02:15 AM
When you have that, best to move the code with Onerror Resume Next into its own procedure.

GTO
09-18-2011, 05:00 AM
When you have that, best to move the code with Onerror Resume Next into its own procedure.

So that the calling procedure picks back up with the current error handling?

Bob Phillips
09-18-2011, 05:08 AM
Exactly. No convoluted coding.

GTO
09-18-2011, 05:37 AM
Thank you :-)