I want to create a UserForm in the module using VBA programmatically. I am a novice and inexperienced so I have tried couple of examples, but they are not fulfilling my requirements.
I just want macro that
- creates a UserForm within a module using VBA
- has a ListBox with some data
- has a CommandButton with a listener
Here is the code which I used
Option Explicit
Sub MakeuserForm()
'Dim CommandButton1 As MsForms.CommandBarButton
'Dim ListBox1 As MsForms.ListBox
Dim UserForm1 As VBComponent
Set UserForm1 = ActiveWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm)
With UserForm1
.Properties("Height") = 100
.Properties("Width") = 200
On Error Resume Next
.Name = "My Form"
.Properties("Caption") = "This is your user form"
End With
ShowForm
End Sub
Sub ShowForm()
NewForm.Show
End Sub
Now I don’t know how to add ListBox and button to the form with a listener.
After hardwork I have found a very simple answer to my question. May help you also.