ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBA code to find macros associated with command buttons in an s/s (https://www.excelbanter.com/excel-programming/404389-vba-code-find-macros-associated-command-buttons-s-s.html)

[email protected]

VBA code to find macros associated with command buttons in an s/s
 
Given a sheet containing many command buttons, how can I find the
macros/subroutines associated with those buttons? (i want to do it in
vba code, rather than right click each button and select "assign
macro") Thanks a lot.

joel

VBA code to find macros associated with command buttons in an s/s
 
A button has a name and a Caption which may be different. this code get both
name and caption.

Sub Getbutton()
For Each but In ActiveSheet.Shapes
If Left(but.Name, 13) = "CommandButton" Then
MsgBox OLEObjects(but.Name).Object.Caption
End If
Next but
End Sub

" wrote:

Given a sheet containing many command buttons, how can I find the
macros/subroutines associated with those buttons? (i want to do it in
vba code, rather than right click each button and select "assign
macro") Thanks a lot.


Dave Peterson

VBA code to find macros associated with command buttons in an s/s
 
First, if you really meant commandbuttons (those are from the control toolbox
toolbar), then you don't assign the macro.

If you doubleclick on one of these commandbuttons while in design mode, you'll
see that it has an event associated with it:

Option Explicit
Private Sub CommandButton1_Click()
End Sub

If you meant buttons from the Forms toolbar, you could use code like:

Option Explicit
Sub testme()

Dim myBTN As Button

For Each myBTN In ActiveSheet.Buttons
If myBTN.OnAction = "" Then
MsgBox "No macro assigned to " & myBTN.Name
Else
MsgBox myBTN.OnAction & vbLf & "is assigned to " & myBTN.Name
End If
Next myBTN

End Sub

wrote:

Given a sheet containing many command buttons, how can I find the
macros/subroutines associated with those buttons? (i want to do it in
vba code, rather than right click each button and select "assign
macro") Thanks a lot.


--

Dave Peterson


All times are GMT +1. The time now is 06:47 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com