|
-
May 23rd, 2005, 12:14 PM
#1
Thread Starter
Hyperactive Member
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.
-
May 23rd, 2005, 01:43 PM
#2
Addicted Member
Re: Excel - beginner here. Simple question ...
VB Code:
Private Sub CommandButton1_Click()
Dim MyButtonName As String
MyButtonName = "Button"
MsgBox "Button Name: " & MyButtonName, vbOKOnly + vbInformation, "Button Name"
End Sub
HTH
if you fail to plan, you plan to fail
-
May 23rd, 2005, 02:12 PM
#3
Thread Starter
Hyperactive Member
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.
-
May 23rd, 2005, 02:36 PM
#4
Fanatic Member
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
-
May 23rd, 2005, 02:40 PM
#5
Thread Starter
Hyperactive Member
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.
-
May 23rd, 2005, 03:02 PM
#6
Fanatic Member
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:
Msgbox "You clicked xxxx button" 'fill in xxxx with the button name
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
-
May 23rd, 2005, 03:14 PM
#7
Thread Starter
Hyperactive Member
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?
-
May 23rd, 2005, 04:10 PM
#8
Addicted Member
Re: Excel - beginner here. Simple question ...
you would need to assign a variable from each button.
e.g.
VB Code:
'global variable
Public Button_CLicked as String
sub Button1_Click()
'assign variable
buttonClicked = "Button1"
'call function
Display_Button_Name
End Sub
sub Button2_Click()
'assign variable
buttonClicked = "Button2"
'call function
Display_Button_Name
End Sub
if you fail to plan, you plan to fail
-
May 24th, 2005, 08:38 AM
#9
Thread Starter
Hyperactive Member
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.
-
May 24th, 2005, 03:00 PM
#10
Thread Starter
Hyperactive Member
Re: Excel - beginner here. Simple question - resolved
I'll go with storing the button name in common storage.
-
May 24th, 2005, 03:15 PM
#11
Fanatic Member
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
-
May 24th, 2005, 03:58 PM
#12
Fanatic Member
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:
Msgbox Application.Caller
This will display the name of the button that is clicked.
VBAhack
-
May 25th, 2005, 02:09 PM
#13
Addicted Member
Re: Excel - beginner here. Simple question ...
nice 1 VBAHack
if you fail to plan, you plan to fail
-
May 26th, 2005, 08:29 AM
#14
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|