➡️ Learn more about WithEvents in VBA.
Step 1: Set Up the UserForm and Class Module
1. Open Excel and save your workbook as .xlsm.
2. Press Alt + F11 to open the VBA Editor.
3. In the Project Explorer,
    a) Right-click your project and click Insert > UserForm.
    b) Right-click your project again and click Insert > Class Module.
Step 2: Add Controls Dynamically in the UserForm
Paste the below code into the UserForm.
Option Explicit Public cbFruit As MSForms.ComboBox Public chkUpdates As MSForms.CheckBox Dim objEventHandler As New clsDynamicEvents Private Sub UserForm_Initialize() ' Create ComboBox Set cbFruit = Me.Controls.Add("Forms.ComboBox.1") With cbFruit .Top = 10 .Left = 10 .Width = 150 .AddItem "Apple" .AddItem "Banana" .AddItem "Cherry" .Name = "cbFruit" End With Set objEventHandler.cbEvents = cbFruit ' Create CheckBox Set chkUpdates = Me.Controls.Add("Forms.CheckBox.1") With chkUpdates .Top = 50 .Left = 10 .Width = 150 .Caption = "Receive Updates" .Name = "chkUpdates" End With Set objEventHandler.chkEvents = chkUpdates End Sub
Step 3: Handle Events in the Class Module
Paste the below code into the Class Module:
Option Explicit Public WithEvents cbEvents As MSForms.ComboBox Public WithEvents chkEvents As MSForms.CheckBox Private Sub cbEvents_Change() MsgBox "You selected: " & cbEvents.Text End Sub Private Sub chkEvents_Click() If chkEvents.Value = True Then MsgBox "You will receive updates." Else MsgBox "You opted out of updates." End If End Sub
Go to the UserForm and run the application. If everthing is done correctly, it will show the UserForm with a dropdown (a ComboBox) and a Checkbox.
Instead of dragging and dropping controls onto the UserForm, we created them using VBA code at runtime.
🚀 Mastering keyboard shortcuts in Excel isn't just about speed, it's about working smarter. In 2025, with more features and data tools packed into Excel than ever before, knowing the right shortcuts can save you hours of clicking and menu-hunting. Whether you're navigating large datasets, formatting cells, or running formulas, these shortcuts help you stay efficient and focused. Think of them as your productivity superpowers.
Check this out... Excel Keyboard Shortcuts You Should Know in 2025
How the Events Work Behind the Scenes
When you run the UserForm, VBA dynamically creates a ComboBox and a CheckBox using code (a Macro), rather than dragging and dropping them in the editor.
👉 The combox box is filled with items like "apple", "banana" etc. When you select a value, the Change event is triggered. This event is written inside a Class Module using the WithEvents keyword.
The event handler responds by showing a message like, "You selected: Apple".
👉 The CheckBox appears with a label like "Receive Updates". When you click the checkbox, the Click event is triggered. It shows a message when you check or uncheck the checkbox.
Why Use a Class Module with WithEvents
1. The Class Module acts like a listener. It watches for actions on the controls.
2. WithEvents allows the class to respond when something happens (like a selection or click).
➡️ Learn more about WithEvents in VBA.
3. This setup keeps your code organized and makes it easier to manage multiple controls and their behaviors.
Conclusion
This example shows how to dynamically add a ComboBox and CheckBox to a UserForm and make them interactive using WithEvents in a Class Module. This technique keeps your code modular, scalable, and clean, especially useful when building flexible forms or dashboards in Excel.