In Excel, I'm adding buttons at runtime on a userform. How do I add these buttons actions to call a sub/function with parameters?
Here is how I currently add the button, which does not accept the 'OnAction' I add it
Dim MyR As Range, MyB As MSForms.Control
For i = 2 To ActiveSheet.UsedRange.Rows.Count
If (Len(ActiveSheet.Cells(i, 1).Value) > 0) Then
Set MyR = Range(ActiveSheet.Cells(i, 1), ActiveSheet.Cells(i, 1))
Set MyB = UserForm1.Controls.Add("Forms.CommandButton.1")
With MyB
.Name = i
.Left = 50
.Top = 25 * i
.Caption = "Caption text"
.OnAction = "SomeFunction"
.Parameter = i
End With
End If
Next i
above code fails on the .OnAction, I can't find how to programmatically add an action here with a parameter different for each button?
UPDATE
Below partially works, but only the last event that has been set works, so only the last button is working as intended
Dim i As Integer
Dim MyB As MSForms.Control
Dim btnEvent As MyCustomButton
For i = 2 To ActiveSheet.UsedRange.Rows.Count
If (Len(ActiveSheet.Cells(i, 1).Value) > 0) Then
Set MyB = UserForm1.Controls.Add("Forms.CommandButton.1")
With MyB
.Name = ActiveSheet.Cells(i, 1).Value
.Caption = ActiveSheet.Cells(i, 1).Value
.Left = 10
.Top = 25 * i
.Width = 75
.Height = 20
.Tag = ActiveSheet.Cells(i, 1).Value
End With
Set btnEvent = New MyCustomButton
Set btnEvent.btn = MyB
Set btnEvent.frm = UserForm1
If (i > 4) Then 'TODO delete this loop
Exit For
End If
End If
Next i
UserForm1.Show
UPDATE 2
Ok, it seems events need to be stored somewhere for them to work if you create multiple of them in a loop. Storing them in a collection solves it for me. Found this in: Excel-VBA Capture all worksheet CommandButton click events to run universal code
Thanks @Maciej Los for your help
If anyone find this usefull, bellow is my working module:
Option Explicit
Sub tests()
Dim i As Integer
Dim MyB As MSForms.Control
Dim btnEvent As MyCustomButton
Dim colButtons As Collection
Set colButtons = New Collection
For i = 2 To ActiveSheet.UsedRange.Rows.Count
If (Len(ActiveSheet.Cells(i, 1).Value) > 0) Then
Set MyB = UserForm1.Controls.Add("Forms.CommandButton.1")
With MyB
.Name = ActiveSheet.Cells(i, 1).Value
.Caption = ActiveSheet.Cells(i, 1).Value
.Left = 10
.Top = 25 * i
.Width = 75
.Height = 20
.Tag = ActiveSheet.Cells(i, 1).Value
End With
Set btnEvent = New MyCustomButton
Set btnEvent.btn = MyB
Set btnEvent.frm = UserForm1
colButtons.Add btnEvent
End If
Next i
UserForm1.Show
End Sub
And the necessary class module:
Public WithEvents btn As MSForms.CommandButton
Public frm As UserForm
Private Sub btn_Click()
Debug.Print (btn.Tag)
End Sub
A userform is needed too, named 'UserForm1' here.