How to Create a UserForm in a Class Module Using VBA
On May 24,2022 by Tom RoutleyCreate a UserForm containing a dynamic number of controls and make sure they are being triggered by certain events. To achieve this result, we will use a UserForm and a class module, and assign any control dynamically created in the userform to the class module. Finally the aim of this tutorial is also to obtain a single module and make the calling function as simple as possible (limited to 2-3 lines of code). Read on for the code.
Prerequisites
Go to Excel Options > Trust Management > Macro Settings and make sure the following option is enabled: Trust access to the VBA project object model.
The code also requires the below references namely the Microsoft Forms 2.0 Object Library and Microsoft Visual Basic For Applications Extensibility 5.3. You can enable them by clicking on the Tools menu of the VBA editor and then on References.
The Code
In this example, we'll create a userform containing two buttons. Upon clicking on these button, their Caption will be displayed in the code of the calling function.
The class module
Create a class module in your VBA project, name it as PremierExemple (ClassName property) and insert this code:
Option Explicit
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'Enable the following references (Tools > References)
'Microsoft Forms 2.0 Object Library
'Microsoft Visual Basic For Applications Extensibility 5.3
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Public maForm As Object 'Userform
Public WithEvents Bouton As MSForms.CommandButton 'Button
Public Dico As Object 'Objet Dictionnary = Object collection
Private Nom As String 'Nom => create or delete userform
Private Sub Class_Initialize()
'create class
Set Dico = CreateObject("Scripting.dictionary")
End Sub
Public Function Value()
'The Value de notre Classe method allow the creation of the user form
'and returns a value
NewUsf "Mon premier UserForm" 'creation of userform
NewBouton "toto", "TOTO", 120, 30, 5, 5 'create the TOTO button
NewBouton "titi", "TITI", 120, 30, 5, 35 'create the TITI button
maForm.Show 'display userform
On Error GoTo fin
Value = maForm.Tag 'assign the value contained in the Tag of the userform to our function.
Unload maForm
Exit Function
fin:
End Function
Private Sub NewUsf(monCaption As String)
'Creation of userform
Set maForm = ThisWorkbook.VBProject.VBComponents.Add(3)
Nom = maForm.Name
VBA.UserForms.Add (Nom)
Set maForm = UserForms(UserForms.Count - 1)
With maForm
.Caption = monCaption
.Width = 150
.Height = 100
End With
End Sub
Public Sub NewBouton(Name As String, Caption As String, Width As Double, Height As Double, Left As Double, Top As Double)
'Creation of a Control Button
Dim Obj
Set Obj = maForm.Controls.Add("forms.CommandButton.1")
If Obj = True Then Exit Sub
Dim cls As New PremierExemple
Set claForm = maForm
Set cls.Bouton = Obj
With cls.Bouton
.Name = Name
.Caption = Caption
.Move Left, Top, Width, Height
End With
Dico.Add Name, cls
Set cls = Nothing
End Sub
Private Sub Bouton_Click()
'event procedure for button click
maForm.Tag = Bouton.Caption
maForm.Hide
End Sub
Private Sub Class_Terminate()
'class deletion
Dim VBComp As VBComponent
Set Dico = Nothing 'delete all instances of our class=> all buttons
If Nom <> "" Then 'if it is the userform (the unique instance having the "Nom" property filled)
Set VBComp = ThisWorkbook.VBProject.VBComponents(Nom) 'search
ThisWorkbook.VBProject.VBComponents.Remove VBComp 'delete
End If
End Sub
The calling function
The procedure for the calling function is greatly simplified... thanks to the class module, you have access to a userform and a Value method. It is returned in a simple way, using the below calling code:
Sub test()
Dim MyForm As New PremierExemple
MsgBox MyForm.Value
Set MyForm = Nothing
End Sub
Image: © 123RFom
Article Recommendations
Latest articles
Popular Articles
Archives
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- January 2021
Leave a Reply