Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

VBA code to find macros associated with command buttons in an s/s

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
peng.gong@gmail.com - 16 Jan 2008 18:18 GMT
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 - 16 Jan 2008 19:14 GMT
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

> 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 - 16 Jan 2008 19:42 GMT
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

> 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.

Signature

Dave Peterson

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.