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!
 

Some videos you may like

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,095
Office Version
2007
Platform
Windows
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]
 

excelvbameche

New Member
Joined
Jul 21, 2019
Messages
2
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,095
Office Version
2007
Platform
Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,101,906
Messages
5,483,654
Members
407,399
Latest member
Rakeforms

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top