Missing code to populate ComboBoxes in VBA

Mel Smith

Well-known Member
Joined
Dec 13, 2005
Messages
1,023
Office Version
  1. 365
Platform
  1. Windows
I have the following code in my UserForm but when I click on the DropDown from within the UserForm on my worksheet, all Comboboxes are blank. What have I done wrong or am I missing some lines of code?

Private Sub UserForm1_Initialize()
With ComboBox1
.AddItem "County Championship"
.AddItem "Royal London One Day Cup"
.AddItem "Vitality T20 Blast"
.AddItem "Second XI Championship"
.AddItem "Second XI 50 Over Friendly"
.AddItem "Second XI T20 (SET20)"
.AddItem "Second XI Friendlies"

With ComboBox2.List = Sheets(1).Range("A4,A6, A8, A10, A12, A14, A16, A18, A20, A22, A24, A26, A28, A30").Value

With ComboBox3.List = Sheets(9).Range("B5:B64").Value

With ComboBox4
.AddItem "First"
.AddItem "Second"
End Sub

mamy thanks,

Mel
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
its not working because you have made the classic error of renaming the event to match your userform name

Rich (BB code):
Private Sub UserForm1_Initialize()
With ComboBox1
.AddItem "County Championship"
.AddItem "Royal London One Day Cup"
.AddItem "Vitality T20 Blast"
.AddItem "Second XI Championship"
.AddItem "Second XI 50 Over Friendly"
.AddItem "Second XI T20 (SET20)"
.AddItem "Second XI Friendlies"

With ComboBox2.List = Sheets(1).Range("A4,A6, A8, A10, A12, A14, A16, A18, A20, A22, A24, A26, A28, A30").Value

With ComboBox3.List = Sheets(9).Range("B5:B64").Value

With ComboBox4
.AddItem "First"
.AddItem "Second"
End Sub

You should NOT rename any of the events - Its UserForm regardless of your forms name

Rich (BB code):
Private Sub UserForm_Initialize()

End Sub

Dave
 
Upvote 0
Hello Dave,

I've removed the "1" from the initialise statement but when i click on the command button (from the worksheet) the userform does not show. Do i need to remove the "1" fro mevrywhere?

Mel

Private Sub UserForm_Click()
UserForm1.Show
End Sub

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "County Championship"
.AddItem "Royal London One Day Cup"
.AddItem "Vitality T20 Blast"
.AddItem "Second XI Championship"
.AddItem "Second XI 50 Over Friendly"
.AddItem "Second XI T20 (SET20)"
.AddItem "Second XI Friendlies"
End With
With ComboBox2.List = Sheets(1).Range("A4, A6, A8, A10, A12, A14, A16, A18, A20, A22, A24, A26, A28, A30").Value
End With
With ComboBox3.List = Sheets(9).Range("B5:B64").Value
End With
With ComboBox4
.AddItem "First"
.AddItem "Second"
End With

End Sub
 
Upvote 0
I've added some more code but still no luck with displaying the UserForm. The following is displayed on 'debug':

Private Sub CommandButton1_Click()
UserForm1.Show "this line is highlighted"
End Sub

This is the code so far from the UF within VBA:

Private Sub UserForm_Click()
UserForm1.Show
End Sub

Private Sub UserForm_Initialize()
With ComboBox1
.AddItem "County Championship"
.AddItem "Royal London One Day Cup"
.AddItem "Vitality T20 Blast"
.AddItem "Second XI Championship"
.AddItem "Second XI 50 Over Friendly"
.AddItem "Second XI T20 (SET20)"
.AddItem "Second XI Friendlies"
End With
With ComboBox2.List = Sheets(1).Range("A4, A6, A8, A10, A12, A14, A16, A18, A20, A22, A24, A26, A28, A30").Value
End With
With ComboBox3.List = Sheets(9).Range("B5:B64").Value
End With
With ComboBox4
.AddItem "First"
.AddItem "Second"
End With

End Sub
Private Sub ComboBox1_Change()
Dim ans As String
ans = ComboBox1.Value
Dim ant As String
ant = ComboBox2.Value
Dim anu As String
anu = ComboBox3.Value
Dim anv As String
anv = ComboBox4.Value
End Sub

Mel
 
Upvote 0
Made some correction to your forms event code as cannot use List property with non contiguous ranges in manner you are using
Code must be placed in your forms code page

VBA Code:
Private Sub UserForm_Initialize()
    Dim cell As Range
  
    With ComboBox1
    .AddItem "County Championship"
    .AddItem "Royal London One Day Cup"
    .AddItem "Vitality T20 Blast"
    .AddItem "Second XI Championship"
    .AddItem "Second XI 50 Over Friendly"
    .AddItem "Second XI T20 (SET20)"
    .AddItem "Second XI Friendlies"
    End With
  
    For Each cell In Sheets(1).Range("A4, A6, A8, A10, A12, A14, A16, A18, A20, A22, A24, A26, A28, A30")
        ComboBox2.AddItem cell.Value
    Next
  
    ComboBox3.List = Sheets(9).Range("B5:B64").Value
  
    With ComboBox4
    .AddItem "First"
    .AddItem "Second"
    End With

End Sub

Form opened & controls populated ok for me - where are you calling it from?

also,
delete this code

VBA Code:
Private Sub UserForm_Click()
UserForm1.Show
End Sub

Dave
 
Upvote 0
hi dave,

i made all the changes, thank you. However, I am still getting the UF not showing. I am trying to call it from the Worksheet via a CommandButton and when in Design Mode the "code" shows: =EMBED("Forms.CommandButton.1",""). when I click on it this is displayed in VB:

Private Sub CommandButton1_Click()
UserForm1.Show "this line is highlighted"
End Sub
 
Upvote 0
In the VB Editor select Tools > Options > General > & check "Break in class module" >Ok
Then try clicking the button again, it should take you the line that is causing the problem.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,496
Members
449,089
Latest member
Raviguru

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top