Search code examples
excelvba

How to add items to Userform combo box and populate in a sheet?


I currently have an Excel spreadsheet which is created to gather my sales staff's time sheet for the day. There are a few things I'd like to do:

  1. I have created a Userform with the required details within the form, but the items in Combo box do not show. Here's the code for it

    Private Sub ComboBox1_Change()
    'combobox1_list
    .AddItem "PRA110AC"
    .AddItem "RAH111AC"
    .AddItem "RAJ112AC"
    .AddItem "MAL113AC"
    .AddItem "Extern"
    
    End Sub
    
  2. I want this data to be pooled to another sheet within the same workbook under appropriate headers. I also want this to be a hidden sheet as this is confidential.

I am new to this so I may have messed this up, but I guess I get a chance ;)


Solution

  • I believe you are in the incorrect Private sub. You are using the event "Change" in the private sub "ComboBox_1." You should be using the event "Initialize" (this event starts when the userform is loaded) in the private sub "Userform" to populate your ComboBox before you see the form. Here's how it should look:

    Private Sub Userform_Initialize()
    
        With Combobox1
             .AddItem "PRA110AC"
             .AddItem "RAH111AC"
             .AddItem "RAJ112AC"
             .AddItem "Extern"
        End With
    End Sub
    

    I hope this helps!