Results 1 to 14 of 14

Thread: Excel - beginner here. Simple question ...

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Excel - beginner here. Simple question ...

    Have a number of buttons on my worksheet. How can I determine and display the name of the button clicked? Thanks.

  2. #2
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Excel - beginner here. Simple question ...

    VB Code:
    1. Private Sub CommandButton1_Click()
    2.  
    3. Dim MyButtonName As String
    4.  
    5. MyButtonName = "Button"
    6.  
    7. MsgBox "Button Name: " & MyButtonName, vbOKOnly + vbInformation, "Button Name"
    8.  
    9. End Sub

    HTH
    if you fail to plan, you plan to fail

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: Excel - beginner here. Simple question ...

    Thanks, but U don't think that helps me. There are several buttons on the spreadsheet. All execute the same code. I need to determine which button was clicked to do the executing. How do I determine the name of the button that was clicked? Thanks.

  4. #4
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Excel - beginner here. Simple question ...

    I think you need to clarify something. If the buttons were created by using the forms toolbar - button, then each button can have a macro assigned to run when the button is clicked (performing a right click on the button and pick Assign Macro to see which macro executes. Also, when you right click a button, the name of the button shows up on the spreadsheet near the upper left corner). If you just want to know which button is clicked (programatically), don't know, maybe there is a button click event or something you could tap into.

    If you are talking about a VBA form (that you define in the VBA IDE), that's another story. In that case, each button has its own button click code and I'm pretty sure there's a me.name (or something) property associated with it.

    Bottom line - which do you have? VBA form or toolbar - form - button?

    VBAhack

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: Excel - beginner here. Simple question ...

    I've tried using the Forms Toolbar and the Control Toolbox. My problem is I don't know how to identify the control (button) that was clicked from within the code that it executes. All of the buttons will execute the same code but the parameter passed to the code will change depending on which button was clicked. Thanks.

  6. #6
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Excel - beginner here. Simple question ...

    OK. So, I think your situation is the following. Each button has a unique macro that executes when the button is clicked. In other words, 5 buttons, 5 separate macros. Each of the 5 macros, calls the exact same routine but passes a different parameter. If this is the case, here's a possibility. Since you can (by right clicking the button) you can find out the button name and the macro it executes, you can put the following in each macro (amount of work depends on how many buttons you have):

    VB Code:
    1. Msgbox "You clicked xxxx button" 'fill in xxxx with the button name
    2. Application.Statusbar = "You clicked xxx button" 'doesn't stop processing

    This essentially hard codes the button name into each macro, which can be some work if you have a lot of buttons. If you really want to do it programatically, don't know, maybe RobDog can help.

    VBAhack

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: Excel - beginner here. Simple question ...

    That would work but I don't want the user to have to enter the button name, or the parameter(s). To make my question simple: What instruction(s) can I put in a macro that is assigned to a button so that the name of that button is displayed when that button is clicked?

  8. #8
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Excel - beginner here. Simple question ...

    you would need to assign a variable from each button.

    e.g.

    VB Code:
    1. 'global variable
    2. Public Button_CLicked as String
    3.  
    4. sub Button1_Click()
    5.  
    6. 'assign variable
    7. buttonClicked = "Button1"
    8.  
    9. 'call function
    10. Display_Button_Name
    11.  
    12. End Sub
    13.  
    14. sub Button2_Click()
    15. 'assign variable
    16. buttonClicked = "Button2"
    17.  
    18. 'call function
    19. Display_Button_Name
    20.  
    21. End Sub
    if you fail to plan, you plan to fail

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: Excel - beginner here. Simple question ...

    Thanks. I see how that could work and I'll use that. I just thought there would be a way to determine which 'shape' was active or selected (i.e., which one was clicked) and then use the 'shape' properties to get the name of the button. I know you can loop through all of the shapes on the sheet and get the name of each one, but how do you know which one has been clicked.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: Excel - beginner here. Simple question - resolved

    I'll go with storing the button name in common storage.

  11. #11
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Excel - beginner here. Simple question ...

    I think you are out of luck unless you use Userforms, which probably means a re-architecture on your part. Your only other hope is to somehow capture the button click event - probably rather involved, if even possible.

    VBAhack

  12. #12
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: Excel - beginner here. Simple question ...

    Wait, I think I found the answer you are looking for. Add the following line in the macro that is executed by each button:

    VB Code:
    1. Msgbox Application.Caller

    This will display the name of the button that is clicked.

    VBAhack

  13. #13
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: Excel - beginner here. Simple question ...

    nice 1 VBAHack
    if you fail to plan, you plan to fail

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2002
    Posts
    382

    Re: Excel - beginner here. Simple question ...

    Now that's what I'm talkin' 'bout! Application.Caller works. Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width