Command Button name

clozed75

New Member
Joined
Dec 13, 2009
Messages
45
Hi,
is it possible to store the current command button's name in a variable ?

e.g.

Private Sub CommandButton3_Click()

how can I refer to "CommandButton3" from here ?

End Sub


note that the button is in a frame and the activecontrol.name doesn't work..it points to the frame

thanks
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
When you say the button's on a frame, is it on a worksheet, or a userform?

Either way, this should work, but as you see I've used the "Me" keyword so you may need to make a longer reference to the button.

As you see, I've demonstrated that the button's name has been stored in a variable, because it can then be referenced from the message box.

Code:
Private Sub CommandButton3_Click()
Dim cmnd_nm As String
cmnd_nm = Me.CommandButton3.Name
MsgBox cmnd_nm
End Sub
 
Upvote 0
thank you for the reply! yes you demonstrate that button's name can be stored in a variable, unfortunaltely my post was very bad written and that is not the goal I wanted..
the point is I would like to address the command button name without specifing the current command button name.. e.g. command button 3.

somthing like "activecontrol" would be good, which can be used in every commanbutton_click sub without referencing directly to the "3, or 4, or 5, etc." which is changing everytime..

by the way, the frame is on a userform..

thank you very much
 
Upvote 0
Code:
Private Sub CommandButton1_Click()
    MsgBox ReallyActiveControl().Name
End Sub

Function ReallyActiveControl() As MSForms.Control
    Set ReallyActiveControl = Me.ActiveControl
    Do Until TypeName(ReallyActiveControl) <> "Frame" And TypeName(ReallyActiveControl) <> "MultiPage"
        If TypeName(ReallyActiveControl) = "Frame" Then
            Set ReallyActiveControl = ReallyActiveControl.ActiveControl
        Else
            With ReallyActiveControl
                Set ReallyActiveControl = .Pages(.Value).ActiveControl
            End With
        End If
    Loop
End Function
 
Upvote 0
You could try this.
Code:
set cmd = Me.ActiveControl.ActiveControl
MsgBox cmd.Name
The first ActiveControl returns the frame as that's the active control of the form, the second the active control in the frame.
 
Upvote 0
You could try this.
Code:
set cmd = Me.ActiveControl.ActiveControl
MsgBox cmd.Name
The first ActiveControl returns the frame as that's the active control of the form, the second the active control in the frame.


Works great..

thank you!
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,258
Members
452,901
Latest member
LisaGo

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