Check to See If Button Was Clicked in Shown UserForm1
Results 1 to 4 of 4

Thread: Check to See If Button Was Clicked in Shown UserForm1
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jul 2019
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Check to See If Button Was Clicked in Shown UserForm1

    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!

  2. #2
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    5,245
    Post Thanks / Like
    Mentioned
    49 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Check to See If Button Was Clicked in Shown UserForm1

    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:
    Private Sub CommandButton1_Click()
      Do This
    End Sub
    Private Sub CommandButton2_Click()
      Do This
    End Sub
    Private Sub CommandButton3_Click()
      Do This
    End Sub
    Regards Dante Amor

  3. #3
    New Member
    Join Date
    Jul 2019
    Posts
    2
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Check to See If Button Was Clicked in Shown UserForm1

    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

  4. #4
    Board Regular DanteAmor's Avatar
    Join Date
    Dec 2018
    Location
    México
    Posts
    5,245
    Post Thanks / Like
    Mentioned
    49 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Check to See If Button Was Clicked in Shown UserForm1

    Quote Originally Posted by excelvbameche View Post
    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
    Regards Dante Amor

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •