1
Thanks

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

Download:
Server 1: Add Control UserForm.xls
1
Thanks

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 Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Related

Original article published by . Translated by jad05. Latest update on by jad05.

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 (https://ccm.net/).

0 Comments