Which userform command button was clicked (VBA)

haseft

Active Member
Joined
Jun 10, 2014
Messages
321
hi,
how do I find wich command button was clicked?
help with this please.
VBA Code:
Dim i As Long
n = 11
For i = 1 To 6
  If UserForm3("cmb" & i) was clicked then ?????
    'do something
  Else
    'do something
  End If
  n = n + 1
Next
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
You can only run code in response to a button's click by assigning it to the click event, in which case you can pass it as an argument to the procedure.
 
Upvote 0
By using the buttons' Click events. I assume that is how you are calling your code now? You've only posted a small segment of your code which makes it hard to comment specifically.
 
Upvote 0
I have a UserForm3 with 6 command button.
If I click to cmb1 then I want to do some procedures.
I just want to know wich command button I clicked in userform.
 
Upvote 0
You have to use the control's click event to trigger the routines. If those routines need to know which button called them (it's not clear why that would be the case), pass it as an argument:

Code:
Private sub cmb1_Click()
macro1 cmb1
macro2
macro3 cmb1
end sub

for example. You would need to modify those routines to take an argument if they don't already.
 
Upvote 0
here is the cod.
VBA Code:
Dim i As Long
n = 11
For i = 1 To 6
  If UserForm3("cmb" & i) was clicked then ?????
     ThisWorkbook.FollowHyperlink Address:=Range(ListBox1.Column(3, ListBox1.ListIndex)).Offset(0, n).Value, NewWindow:=True
  End If
  n = n + 1
Next
 
Upvote 0
It looks like you could simply pass a number as the argument rather than the actual button object. If you need help with that, please post the full code including the procedure declaration.
 
Upvote 0
In userform there is only 6 command button, nothing else. .
found out wich one is clicked?
VBA Code:
Dim i As Long
For i = 1 To 6
  If UserForm3("cmb" & i) was clicked then ?????
     MsgBox " Button &" cmb & i" was clicked"
  End If
Next
 
Upvote 0
I join with RoryA in wanting to see your full code, but you might look at the userform's ActiveControl property to see which button was clicked. Its not super reliable (depending on the TakeFocusWhenClicked property of the command buttons).
 
Upvote 0

Forum statistics

Threads
1,215,737
Messages
6,126,570
Members
449,318
Latest member
Son Raphon

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