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 8482347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:48:09+00:00 2026-06-10T19:48:09+00:00

I am trying to add a combo box to a user form which will

  • 0

I am trying to add a combo box to a user form which will be created at run time , the problem I am facing is to add items to the combo box? Not able to figure out where the mistake would be. Thanks.

    Function addComboBox(ByRef TempForm As Object, ByVal controlType As String, 
ByVal pos As Integer, ByVal strCaption As String, ByVal strValues As String)

     Dim NewComboBox As MSforms.ComboBox
     Dim arr As Variant
     Dim i As Integer

     Set NewComboBox = TempForm.Designer.Controls.Add("forms.ComboBox.1")
      arr = Split(strValues, ";")


        With NewComboBox
                .Name = strCaption & "_" & controlType & "_" & pos
                .Top = 20 + (12 * pos)
                .Left = 100
                .Width = 150
                .Height = 12

        End With



      For i = 0 To UBound(arr)

       NewComboBox.AddItem arr(i)

      Next i

    End Function
  • 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-10T19:48:11+00:00Added an answer on June 10, 2026 at 7:48 pm

    Remove the word Designer

    Try this (Tried And Tested)

    Set NewComboBox = TempForm.Controls.Add("Forms.ComboBox.1")
    

    FOLLOWUP

    Try this. (TRIED AND TESTED)

    Option Explicit
    
    Sub Sample()
        Dim TempForm As Object
        Dim Ret
    
        Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    
        Ret = addComboBox(TempForm, "CBox", 1, "MyCombo", "1;2;3;4")
    
        VBA.UserForms.Add(TempForm.Name).Show
    End Sub
    
    Function addComboBox(ByRef TempForm As Object, ByVal controlType As String, _
    ByVal pos As Integer, ByVal strCaption As String, ByVal strValues As String)
    
        Dim NewComboBox As MSForms.ComboBox
        Dim n As Long, nLines As Long, i As Long
        Dim arr As Variant
    
        Set NewComboBox = TempForm.designer.Controls.Add("Forms.ComboBox.1")
        arr = Split(strValues, ";")
    
    
        With NewComboBox
            .Name = strCaption & "_" & controlType & "_" & pos
            .Top = 20 + (12 * pos)
            .Left = 10
            .Width = 150
            .Height = 12
        End With
    
        n = 2
    
        With TempForm
            nLines = .CodeModule.CountOfLines
            .CodeModule.InsertLines nLines + 1, "Private Sub UserForm_Initialize()"
            For i = 0 To UBound(arr)
                .CodeModule.InsertLines nLines + n, "    " & _
                NewComboBox.Name & ".AddItem " & arr(i)
                n = n + 1
            Next i
            .CodeModule.InsertLines nLines + n, "End Sub"
        End With
    End Function
    

    SCREENSHOT

    enter image description here

    MORE FOLLOWUP

    Thanks for the solution , In case if I have to call addComboBox more than once, i.e to add two or more combo boxes , UserForm_Initialize sub will be created more than once, which is problem again. – Vikram

    In such a scenario you have to check if the UserForm_Initialize proc exists and then parse it. See the code below. I have added a new optional parameter S to your function. I am using that to place the combos one below the other.

    Option Explicit
    
    Sub Sample()
        Dim TempForm As Object
        Dim Ret
    
        Set TempForm = ThisWorkbook.VBProject.VBComponents.Add(3)
    
        Ret = addComboBox(TempForm, "CBox", 1, "MyCombo", "1;2;3;4")
    
        Ret = addComboBox(TempForm, "CBox1", 1, "MyCombo1", "5;6;7;8", 20)
    
        Ret = addComboBox(TempForm, "CBox2", 1, "MyCombo2", "9;10;11;12", 40)
    
        VBA.UserForms.Add(TempForm.Name).Show
    End Sub
    
    Function addComboBox(ByRef TempForm As Object, ByVal controlType As String, _
    ByVal pos As Integer, ByVal strCaption As String, ByVal strValues As String, _
    Optional s As Long)
    
        Dim NewComboBox As MSForms.ComboBox
        Dim n As Long, nLines As Long, i As Long, uInitLine As Long
        Dim arr As Variant
        Dim MyModule As Object
    
        Set NewComboBox = TempForm.Designer.Controls.Add("Forms.ComboBox.1")
        arr = Split(strValues, ";")
    
        With NewComboBox
            .Name = strCaption & "_" & controlType & "_" & pos
            .Top = 20 + (12 * pos) + s
            .Left = 10
            .Width = 150
            .Height = 12
        End With
    
        '~~> Connect to the code module of the Userform
        Set MyModule = ThisWorkbook.VBProject.VBComponents(TempForm.Name).CodeModule
    
        '~~> Check if there is a procedure called UserForm_Initialize
        On Error Resume Next
        uInitLine = MyModule.ProcStartLine("UserForm_Initialize", 0)
        On Error GoTo 0
    
        With TempForm
            '~~> UserForm_Initialize Found
            If uInitLine > 0 Then
                nLines = uInitLine + 2: n = 0
                For i = 0 To UBound(arr)
                    .CodeModule.InsertLines nLines + n, "    " & _
                    NewComboBox.Name & ".AddItem " & arr(i)
                    n = n + 1
                Next i
            Else
                n = 2
    
                nLines = .CodeModule.CountOfLines
    
                .CodeModule.InsertLines nLines + 1, "Private Sub UserForm_Initialize()"
                For i = 0 To UBound(arr)
                    .CodeModule.InsertLines nLines + n, "    " & _
                    NewComboBox.Name & ".AddItem " & arr(i)
                    n = n + 1
                Next i
                .CodeModule.InsertLines nLines + n, "End Sub"
            End If
        End With
    End Function
    

    SCREENSHOT (Of Userform)

    enter image description here

    SCREENSHOT (Of Userform Code)

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to add a 'box' to a form at design time, I looked up
I am trying to add a drop down combo box with a drop down
I am trying to add 3 combo boxes which all display the exact same
I want to display member of combo box(add) in windows form before clicking drop
I'm creating a dynamic combo box and adding to a form. I'm trying to
I'm trying to add items to a listbox,combobox, radiolist using reflection. The code I
I am trying to populate a combo list box with the results of a
hi i am trying to populate a combo box in struts 1.2 but i
I have an array of objects that I'm trying to add to the Items
I am trying to get all the data from a combo-box in vb. Lets

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.