Finding form command buttons

Glove303

Board Regular
Joined
Dec 18, 2010
Messages
65
I have some code that searches for form command buttons by going through all the Shapes on a sheet, and checking their name.

This is not particularly efficient. The code looks like this:

Dim Sh As Shape

With Trades
For Each Sh In .Shapes
If Mid(Sh.Name, 2, 3) = "btn" Then
Sh.Delete
End If
Next Sh
End With


Is there a way to loop just through form command buttons?
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can do this :-
Code:
Buttons.Delete

Or this:-
Code:
Dim B As Button
For Each B In ActiveSheet.Buttons
   B.Delete
Next B
 
Upvote 0
Code:
    Dim btn As Button
    
    With Trades
        For Each btn In .Buttons
            [COLOR="Green"]'Do something to each button[/COLOR]
            btn.Select
        Next btn
    End With
    
    [COLOR="Green"]'Delete all buttons[/COLOR]
    Trades.Buttons.Delete
 
Upvote 0
Great, thanks people.

So out of interest, how do a refer specifically to an activex command button?
 
Upvote 0
Code:
    Dim oleCmb As OLEObject
    
    [COLOR="Green"]' Loop through each ActiveX command button[/COLOR]
    With Trades
        For Each oleCmb In .OLEObjects
            If TypeName(oleCmb.Object) = "CommandButton" Then
                [COLOR="Green"]' Do somthing with each ActiveX Command Button[/COLOR]
                oleCmb.Select
            End If
        Next oleCmb
    End With
    
    [COLOR="Green"]' Reference a command button by Name[/COLOR]
    Trades.OLEObjects("CommandButton1").Select

I don't think there is a built-in collection specific to ActiveX command buttons as there is for Forms type command buttons.
 
Upvote 0

Forum statistics

Threads
1,224,600
Messages
6,179,836
Members
452,947
Latest member
Gerry_F

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