VBA test to check if a specific Button (Form Control) was clicked

bellg

New Member
Joined
May 13, 2014
Messages
29
I have a button (form control, not ActiveX). I renamed this button "butVA03". I also have 15 other buttons in my excel sheet, but I want a test to check if "butVA03" was the button that I clicked.

Is this possible? My suspicion is that I'm not defining "butVA03" correctly as a specific button, but I can't figure out how to do this. Thanks in advance.

Code:
Public Sub AllButtons()

If butVA03 = True Then
MsgBox ("True")
Else
MsgBox ("False")
End If

End Sub
 
Last edited:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Have you used the Click event? One of the forms I use for myself has the following code in it:

Code:
Private Sub cmdSaveAdd_Click()
      [COLOR=#008000]'Insert code here.[/COLOR]
End Sub
 
Upvote 0
Thanks Mike! That works beautifully.

Quick question on your comment "if that routine is called by the calling macro". I'm not understanding when it will error out as the code below works for me. Do you have an example of your aforementioned warning?
Code:
Public Sub specsVA03()
Call AllButtons
End Sub

Public Sub AllButtons()


    If Application.Caller = "butVA03" Then
        MsgBox "true"
    Else
        MsgBox "False"
   End If


End Sub
 
Upvote 0
It will error when Application.Caller is not a string. If you read the link, it will explain what kinds of values Application.Caller returns under different situations.

I should note that Application.Caller is determined by how VBA is entered. In your testing, if specsVA003 was called from a Button (or other forms control) then there would be no error.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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