Ultimate VBA code for commandbar control

Kelvin Stott

Active Member
Joined
Oct 26, 2010
Messages
338
Hello to all,

I need a very short and simple piece of code that disables all commandbar controls based on a pre-defined list of ID numbers. I imagine it would have the following basic structure:

1. Define a list of commandbar control IDs (19, 21, 22, etc.)
2. Disable each commandbar control with ID in pre-defined list

Maybe something like this:

Code:
CBCList = (19, 21, 22, etc.)
For Each CB in CBCList
       Commandbar(CB).Enabled = False
Next

Of course this "code" will not work, however I don't know enough VBA to write something that works.

Could somebody please help me with this, as I imagine this would be very useful to lots of other people also.

Many thanks,
Kelvin
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi there,

You are nearly there..

Code:
Sub test()
    CBCList = Array(17, 16, 15, 19, 21, 22, 24)
    For Each CB In CBCList
        Application.CommandBars(CB).Enabled = False
    Next
End Sub
 
Upvote 0
Many thanks!

Your code didn't quite work as the numbers in the list/array weren't recognized as CommandBarControl ID numbers, however I was able to play around with it and the following works like a charm:

Code:
    CBCList = Array(19, 21, 22)
    Dim CBC As CommandBarControl
    
    On Error Resume Next
    For Each CBCID In CBCList
    For Each CBC In CommandBars.FindControls(ID:=CBCID)
        CBC.Enabled = False
    Next
    Next

Essentially you can use this code to disable all commandbar controls with ID numbers defined in the array, so just what I was looking for! :):):)

Thanks again,
Kelvin
 
Upvote 0
Is there a list of control ID numbers that one can refer to ?

for excel as well as VB commandbars... (Especially: Remove sheet in the File menu of VB Editor... I seem to have lost that... :( )

further, what exactly is the correct coding for something like


Application.Commandbars("File").Controls("Print").enabled =false
Doesn't work for me in Excel 2003... even if I try

commandbars("Worksheet Menu Bar").controls("&File").Controls("&Print").enabled = false

cheers

Tom
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,703
Members
452,938
Latest member
babeneker

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