troubleshooting Question

VBA - how to make a Button refer to itself in its own click event in a generic way

Avatar of FaheemAhmadGul
FaheemAhmadGulFlag for United Kingdom of Great Britain and Northern Ireland asked on
VBA
4 Comments1 Solution79 ViewsLast Modified:
I am wondering if there is a way a CommandButton on a VBA userForm can refer to itself in its own click event.
To clarify my question, I have a button named thisButton on my form. In its click event I would like to display a message box that will display its own caption in a message box. I can currently do this as I show in my code sample by saying thisButton.Caption but this means that if I want to do the same in the click event of another button named thatButton, then I have to say thatButton.Caption.
That is each button has to use its own name in the code to refer to itself.

In .NET applications the ClickEvent of every button has a parameter named sender that a button can use to refer to itself. I am wondering if such a thing is possible in VBA.
Thank you for your help.



Private Sub thisButton_Click()

MsgBox thisButton.Caption

End Sub

Private Sub thatButton_Click()

MsgBox thatButton.Caption

End Sub


' I would like to do this in the following way, if at all that kind of thing is possible

Private Sub thisButton_Click()
'' assuming sender here refers to the button that is being clicked
MsgBox sender.Caption

End Sub

Private Sub thatButton_Click()
' assuming sender here refers to the button that is being clicked
MsgBox sender.Caption

End Sub
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros