Hi Raymundus and welcome to ExcelForum,
One way to do what you want is to use one Class Event Handler to process all your CommandButtons. See the attached file which contains the following code for a simple example using 4 CommandButtons.
In Class Module ClassButtonEvent (The Name of the module must be ClassButtonEvent or changes to the code are required):
Option Explicit
'This code is based on code from Andy Pope (Thank you Andy)
'http://www.excelforum.com/excel-programming-vba-macros/1021112-call-userform-from-a-variable-number-of-activex-command-buttons.html
Public WithEvents myCommandButton As MSForms.CommandButton
Private Sub myCommandButton_Click()
'This is the Active X Event Handler
MsgBox "Clicked " & Me.myCommandButton.Name & " " & Me.myCommandButton.Caption
End Sub
In an ordinary code module:
Option Explicit
'This code is based on code from Andy Pope (Thank you Andy)
'http://www.excelforum.com/excel-programming-vba-macros/1021112-call-userform-from-a-variable-number-of-activex-command-buttons.html
Private Const myCommandButtonCOUNT As Long = 4
Private Const sControlTypeDESCRIPTION As String = "Active X CommandButton"
Private Const sControlType As String = "CommandButton"
Public myCommandButtonEvents As Collection
Sub CreateCommandButtons()
'This Creates 'Active X' Controls
Dim Output As Range
Dim TempButt As OLEObject
Dim iCount As Long
Dim iIndex As Long
'First Control will be at the 'Top Left' of the following cell
Set Output = ActiveSheet.Range("B4")
For iIndex = 1 To myCommandButtonCOUNT
'Create the Command Button
Set TempButt = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=Output.Left, _
Top:=Output.Top, _
Width:=Output.Width * 3, _
Height:=Output.Height * 3)
'Create the 'Caption' for each CommandButton
TempButt.Object.Caption = "Active X CommandButton " & iIndex
'Increment the 'Top Left' for the next CommandButton
Set Output = Output.Offset(4, 0)
'Increment the Control Created Count
iCount = iCount + 1
Next
Debug.Print iCount & Format(sControlTypeDESCRIPTION, " @") & " Control(s) were Created."
End Sub
Sub IterateThruCommandButtons()
'This iterates through 'Active X' Controls
Dim myObject As OLEObject
Dim iCount As Long
For Each myObject In ActiveSheet.OLEObjects
If TypeName(myObject.Object) = "CommandButton" Then
iCount = iCount + 1
Debug.Print Format(iCount, "000 ") & _
Format(myObject.Name, "!@@@@@@@@@@@@@@@@@@ ") & _
Format(myObject.Object.Caption, "!@@@@@@@@@@@@@@@@@@@@@@@@@@ ") & _
Format(myObject.Top, "@@@@@@@ ") & _
Format(myObject.Left, "@@@@@@@ ")
End If
Next myObject
If iCount = 0 Then
Debug.Print "There were NO" & Format(sControlTypeDESCRIPTION, " @") & " CONTROLS to Iterate through."
End If
End Sub
Sub EnableCommandButtonEvents()
'This Enables Active X Events (Active X controls must already exist)
Dim CmdBtnEvents As ClassButtonEvent
Dim myObject As OLEObject
Dim iCount As Long
Dim iIndex As Long
'Define the Event Collection
Set myCommandButtonEvents = New Collection
'Create the Events
For Each myObject In ActiveSheet.OLEObjects
iIndex = iIndex + 1
If TypeName(myObject.Object) = sControlType Then
Set CmdBtnEvents = New ClassButtonEvent
Set CmdBtnEvents.myCommandButton = ActiveSheet.OLEObjects(iIndex).Object
myCommandButtonEvents.Add CmdBtnEvents, CStr(myCommandButtonEvents.Count + 1)
iCount = iCount + 1
If iCount >= myCommandButtonCOUNT Then
Exit For
End If
End If
Next myObject
Debug.Print iCount & Format(sControlTypeDESCRIPTION, " @") & " Control Event(s) were Enabled."
End Sub
Sub DisableCommandButtonEvents()
'This Disables Active X Events
Dim iCount As Long
Dim iStartingControlEventCount As Long
'Test to see if Controls Exist
On Error Resume Next
iStartingControlEventCount = myCommandButtonEvents.Count
On Error GoTo 0
'Disable Controls
If iStartingControlEventCount > 0 Then
Do While myCommandButtonEvents.Count > 0
myCommandButtonEvents.Remove 1
iCount = iCount + 1
Loop
End If
Debug.Print iCount & Format(sControlTypeDESCRIPTION, " @") & " Control Event(s) were Disabled."
'Clear object pointer
Set myCommandButtonEvents = Nothing
End Sub
Sub DeleteCommandButtons()
'This deletes 'Active X' Controls
Dim myObject As OLEObject
Dim iCount As Long
For Each myObject In ActiveSheet.OLEObjects
If TypeName(myObject.Object) = sControlType Then
iCount = iCount + 1
myObject.Delete
End If
Next myObject
Debug.Print iCount & Format(sControlTypeDESCRIPTION, " @") & " Control(s) were Deleted."
End Sub
Lewis
Bookmarks