Method/code to determine which button was clicked

MassSpecGuru

Board Regular
Joined
Feb 23, 2010
Messages
55
I have an Excel 2003 workbook that has many buttons on different worksheets within it and I was wondering if there is a way to tell which button was clicked?
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
If they are "Form" buttons and all calling the same procedure you can use:

Code:
MsgBox Application.Caller

Gary
 
Upvote 0
If they are "Form" buttons and all calling the same procedure you can use:

Code:
MsgBox Application.Caller

Gary


Thanks for the reply, Gary - and yes, they are form buttons - but how do I get the Button name into a variable as opposed to displayed in a message box?
 
Upvote 0
I usually use "Select Case" for something like this:

Code:
Public Sub Button_Click()

Select Case Application.Caller

    Case "Button 1" 'Use actual button names
        'code for button 1
        MsgBox "Button 1 clicked"
    Case "Button 2"
        'code for button 2
    'case "More buttons"
        
    Case Else
        'Any other buttons that do the same thing
End Select

End Sub

Or if you prefer:


Code:
Dim sButton As String

sButton = Application.Caller

MsgBox sButton

Gary
 
Upvote 0
I usually use "Select Case" for something like this:

Code:
Public Sub Button_Click()
 
Select Case Application.Caller
 
    Case "Button 1" 'Use actual button names
        'code for button 1
        MsgBox "Button 1 clicked"
    Case "Button 2"
        'code for button 2
    'case "More buttons"
 
    Case Else
        'Any other buttons that do the same thing
End Select
 
End Sub

Or if you prefer:


Code:
Dim sButton As String
 
sButton = Application.Caller
 
MsgBox sButton

Gary


Thanks, Gary, that works just fine!

Actually, while waiting for a reply I tried your second suggestion myself and it worked... well, when run from within Excel. Whenever I try it or your previous suggestion (either with or without debug. print) from within the VB Editor window I get a:

"Run-time error '13': Type mismatch"

error message.

Any ideas how to fix that?

MSG
 
Upvote 0
I don't think I know what you mean. Although I dimensioned it as a string, Application.Caller returns a variant. It can contain more than just the name of the control including some error info.

Have a look in help at Application.Caller. It explains it better than I can.

Gary
 
Upvote 0
Whenever I try it or your previous suggestion (either with or without debug. print) from within the VB Editor window I get a:

"Run-time error '13': Type mismatch"

error message.

After reading your last post more carefully (running within IDE), that error would be expected. Application.Caller is "initialized" by the form button click where its parameters are set up and passed to the procedure being called. If you run it (the called procedure) from within the IDE you have bypassed that step.

Gary
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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