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
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