Check to See If Button Was Clicked in Shown UserForm1

excelvbameche

New Member
Joined
Jul 21, 2019
Messages
2
Hi - I'm trying to figure out how to run a line of code if one of three buttons was selected in a shown UserForm1.

This is what I have so far:

Private Sub CommandButton1_Click()

If UserForm1.CommandButton1.TakeFocus******* Then


Do This


ElseIf UserForm1.CommandButton2.TakeFocus******* Then


Do This


ElseIf UserForm1.CommandButton3.TakeFocus******* Then

Do This


End If

End Sub

...

User Form:

Private Sub CommandButton1_Click()


UserForm1.Hide


End Sub


Private Sub CommandButton2_Click()


UserForm1.Hide


End Sub


Private Sub CommandButton3_Click()


UserForm1.Hide


End Sub

Not sure if the ".TakeFocus*******" handle is the way to go, or what I'm doing wrong in general.

Thank you for your time!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
At the moment of pressing the button you already have the control on the button itself, in addition the button is the selected control, it is not necessary to ask which button has the focus.

You need somethig like this:

Code:
[COLOR=#333333]Private Sub CommandButton1_Click()[/COLOR]
[COLOR=#333333]  Do This[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
[COLOR=#333333]Private Sub CommandButton2_Click()[/COLOR]
[COLOR=#333333]  Do This[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
[COLOR=#333333]Private Sub CommandButton3_Click()[/COLOR]
[COLOR=#333333]  Do This[/COLOR]
[COLOR=#333333]End Sub[/COLOR]
 
Upvote 0
Thank you Dante!

What if I want to embed an If-Then statement in the code that references which button got clicked in the UserForm.

Thanks
 
Upvote 0
Thank you Dante!

What if I want to embed an If-Then statement in the code that references which button got clicked in the UserForm.

Thanks


Why do you want to embed the statement?

It is not necessary, because if you pressed button1, in button1 you know that you pressed button1:

Code:
Private Sub CommandButton1_Click()
  msgbox "You pressed the button1"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

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