How to pass variables into dynamically created command buttons

KurtEdw

New Member
Joined
Feb 24, 2021
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi There,

I have created some dynamic command buttons that are created on the fly and have created a class module for them to execute their respective code.

I have got it working to execute code from these dynamic command buttons, but am having trouble passing variables from the userform into the class modules.

Any ideas/help would be appreciated

Thanks Kurt


Userform Code:
~~~~~~~~~~~~~~~~~~~~~~~~~~~
VBA Code:
Option Explicit

Public MyEvents As New Collection

Private Sub UserForm_Initialize()

    Dim tmpCtrl As Control
    Dim CmbEvent As clsMyEvents
    Dim x As Long

    'Add some dummy data for the combo-boxes.
    Sheet1.Range("A1:A5") = Application.Transpose(Array("Red", "Yellow", "Green", "Blue", "Pink"))
    Sheet1.Range("B1:B5") = Application.Transpose(Array(1, 2, 3, 4, 5))
    Sheet1.Range("C1:C5") = Application.Transpose(Array(5, 4, 3, 2, 1))


    Next x

    For x = 1 To 5
        Set tmpCtrl = Me.Controls.Add("Forms.CommandButton.1", "MyButton" & x)
        With tmpCtrl
            .Left = 100
            .Width = 50
            .Height = 20
            .Top = (x * 20) - 18
            .Caption = "Num " & x
        End With

        kk = "test"
        Set CmbEvent = New clsMyEvents
        Set CmbEvent.MyButton = tmpCtrl
        MyEvents.Add CmbEvent(kk)
    Next x

End Sub



~~~~~~~~~~~~~~~~~~

Class Module Code
VBA Code:
Option Explicit

Public WithEvents MyCombo As MSForms.ComboBox
Public WithEvents MyButton As MSForms.CommandButton


Private Sub MyButton_Click(kk)

'    Dim BtnNum As Long
'
'    BtnNum = Replace(MyButton.Name, "MyButton", "")

'    MsgBox MyButton.Name & " is " & IIf(BtnNum Mod 2 = 0, "even", "odd")

'MsgBox ("test" & CStr(i))

MsgBox (kk)

End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
CmbEvent.ButtonNo = i

Create public variable in class as "ButtonNo"
 
Upvote 0
you can refernce the userform from the class, so if you imbed your variables on the userform in invisible labels or whatever, you can reference them directly in the class. eg

VBA Code:
Private Sub MyButton_Click()
    With MyUserFormName
        msgbox MyButton.caption & " button sends" .Label1.Caption & " value from the UDF"
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,593
Messages
6,120,434
Members
448,961
Latest member
nzskater

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