A few words of thanks would be greatly appreciated.

VBA - Adding controls dynamically into a Userform

To create support for this demo , we used the calculator example, but this time creating all the controls dynamically.

When you create controls dynamically on a Userform, Excel does not recognize the names of the controls. This means that events generated by your controls and their properties will not be accessible via the assigned names. For example, when you create a button with the following code:

Set Bouton = Me.Controls.Add("Forms.CommandButton.1", "Bt" & i, True)

The name of the button will be (in the case that i=1)BT1

But the Private Sub BT1_Click() event will never be generated. To handle that issue you used a collections of objects for properties and collections of classes for events. Collections of objects not only contain the control but also a key to reference it. By configuring the Tag property of controls with the desired index, the collection of class can return an index that also allows you know which control has been the subject of this event.

There is no control in the design of the UserForm. All controls are dynamically created the calculator, including the Frames Container. The demo also gives a way to incorporate the buttons in frames

Server 1: Add Control UserForm.xls

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.


This document, titled « VBA - Adding controls dynamically into a Userform », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).
1 vote - 5.0/5

1 Comment

Been looking a long time for something this clear and concise with reasonable complexity.

thank you
I am in your dept.