Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8258115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:25:39+00:00 2026-06-08T02:25:39+00:00

I want to create a UserForm in the module using VBA programmatically. I am

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T02:25:41+00:00Added an answer on June 8, 2026 at 2:25 am

    After hardwork I have found a very simple answer to my question. May help you also.

    Sub CreateUserForm()
    Dim myForm As Object
    Dim NewFrame As MSForms.Frame
    Dim NewButton As MSForms.CommandButton
    'Dim NewComboBox As MSForms.ComboBox
    Dim NewListBox As MSForms.ListBox
    'Dim NewTextBox As MSForms.TextBox
    'Dim NewLabel As MSForms.Label
    'Dim NewOptionButton As MSForms.OptionButton
    'Dim NewCheckBox As MSForms.CheckBox
    Dim X As Integer
    Dim Line As Integer
    
    'This is to stop screen flashing while creating form
    Application.VBE.MainWindow.Visible = False
    
    Set myForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    
    'Create the User Form
    With myForm
        .Properties("Caption") = "New Form"
        .Properties("Width") = 300
        .Properties("Height") = 270
    End With
    
    'Create ListBox
    Set NewListBox = myForm.designer.Controls.Add("Forms.listbox.1")
    With NewListBox
        .Name = "lst_1"
        .Top = 10
        .Left = 10
        .Width = 150
        .Height = 230
        .Font.Size = 8
        .Font.Name = "Tahoma"
        .BorderStyle = fmBorderStyleOpaque
        .SpecialEffect = fmSpecialEffectSunken
    End With
    
    'Create CommandButton Create
    Set NewButton = myForm.designer.Controls.Add("Forms.commandbutton.1")
    With NewButton
        .Name = "cmd_1"
        .Caption = "clickMe"
        .Accelerator = "M"
        .Top = 10
        .Left = 200
        .Width = 66
        .Height = 20
        .Font.Size = 8
        .Font.Name = "Tahoma"
        .BackStyle = fmBackStyleOpaque
    End With
    
    'add code for listBox
    lstBoxData = "Data 1,Data 2,Data 3,Data 4"
    myForm.codemodule.insertlines 1, "Private Sub UserForm_Initialize()"
    myForm.codemodule.insertlines 2, "   me.lst_1.addItem ""Data 1"" "
    myForm.codemodule.insertlines 3, "   me.lst_1.addItem ""Data 2"" "
    myForm.codemodule.insertlines 4, "   me.lst_1.addItem ""Data 3"" "
    myForm.codemodule.insertlines 5, "End Sub"
    
    'add code for Comand Button
    myForm.codemodule.insertlines 6, "Private Sub cmd_1_Click()"
    myForm.codemodule.insertlines 7, "   If me.lst_1.text <>"""" Then"
    myForm.codemodule.insertlines 8, "      msgbox (""You selected item: "" & me.lst_1.text )"
    myForm.codemodule.insertlines 9, "   End If"
    myForm.codemodule.insertlines 10, "End Sub"
    'Show the form
    VBA.UserForms.Add(myForm.Name).Show
    
    'Delete the form (Optional)
    'ThisWorkbook.VBProject.VBComponents.Remove myForm
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want create object from Canvas3D class(in java) but my Compiler doesn't have this
I want create subdomains like this: domain.com/type/city An examples: domain.com/restaurants/new_york domain.com/hotels/new_york domain.com/restaurants/chicago I have
i want create image animation , i have 50 images with png format now
I want create module which update list of usb devices automatically (not only mass
I want create a pdf using iText. The method which does this is a
I want create a DataSet class which is basically a list of samples. But
I want create own SetupTypeDlg using wix default dialogs to show the disk costs
I want create a site by command line using appcmd. How can I associate
I have following string. $option = '+'; $value = '100'; I want create new
i want create multiple search where statement $where_search is a multiple condition from post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.