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
 
I can’t see that I have Class1 on the menu.
 

Attachments

  • Picture1.png
    Picture1.png
    72.2 KB · Views: 9
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I am really sorry DanteAmor,
Different language on VBA (Class1, Klass1).
It works greate.
Tanks again for your support.

One more issue:
Is it possible to exclude one or more CommandButton?
For example: get massage when you press CommandButton with the name cmb1, cmb2 and cmb3, but not for cmb4, cmb5 etc (all are in the same userform).
 
Upvote 0
Try this:
In the Case line write the names of the buttons you want to include
Case "CommandButton1", "CommandButton2", "CommandButton4"

VBA Code:
Dim colButton As Collection 'Collection of Button      'This to the top of all the code.

Private Sub UserForm_Initialize()
  Dim ctrl As MSForms.Control
  Dim clsObject As Class1
  
   'Create New Collection To Store Custom
  Set colButton = New Collection
  For Each ctrl In Me.Controls
    If TypeName(ctrl) = "CommandButton" Then
      Dim m
      m = ctrl.Name
      Select Case ctrl.Name
        Case "CommandButton1", "CommandButton2", "CommandButton4"
          Set clsObject = New Class1
          Set clsObject.Button = ctrl
          colButton.Add clsObject
      End Select
    End If
  Next
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,216,814
Messages
6,132,849
Members
449,761
Latest member
AUSSW

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