Adding controls to a frame in an Excel userform with VBA

后端 未结 3 788
眼角桃花
眼角桃花 2020-12-06 06:00

I need to create labels and buttons dynamically and then add them to a frame within a userform. How do I do this? Seems like it should be easier than it really is.

3条回答
  •  半阙折子戏
    2020-12-06 06:22

    My variation on the theme above. This is just for a 4x4 array of buttons though. Create a userform and add this to its code. The same concepts can be used with your labels (or see the previous answer):

    Private cmdLots(20) As MSForms.CommandButton
    
    Private Sub UserForm_Initialize()
    For i = 1 To 4
    For j = 1 To 4
        k = i + (4 * j)
        Set cmdLots(k) = UserForm2.Controls.Add("Forms.CommandButton.1", "cmd1")
        With cmdLots(k)
            .Top = i * 25
            .Left = (j * 80) - 50
            .BackColor = RGB(50 * i, 50 * j, 0)
            .Caption = "i= " & i & "  j= " & j
        End With
    Next j
    Next i
    End Sub
    

提交回复
热议问题