(Solved) – Affect a macro to button click on a button created by a macro


I want to affect a macro to a button I generate in an userform with a macro.

Public List As Collection
Private Sub UserForm_Initialize()
Call LoadData(ThisWorkbook.TESTS)

Dim i As Integer
Dim cmdbtn As Object

For i= 1 To List.Count
Set cmdbtn = UserForm1.Controls.Add("Forms.CommandButton.1")

With cmdbtn
    .Caption = List(i)

    If (i* 20   2) > 450 Then
        .Left = 130
        .Top = (i - 20) * 20   2
        .Width = 102
        .Left = 12
        .Top = i* 20   2
        .Width = 102
    End If
    .OnAction = "filepathfile.xlsm!MacroName" ''does not work
    .click macroname ''does not work
End With
Next i   
End Sub

The LoadData fuction fill the collection named List.

the macro I want to call is located in the userform itself, the userform is in the same workbook from where data is “loaded”.

How to achieve that. I keep finding how to affect a macro to a button when googling.

  • Extra:
    If you can indicate me a good ressource on how to do a nice tile design from one incrementing integer only and unknown full size.

Leave a Reply

Your email address will not be published. Required fields are marked *