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
 
Definitely out of Design Mode. Total VB code:

1583676162927.png


and:

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").Value
ComboBox2.AddItem cell.Value
Next
ComboBox3.List = Sheets(9).Range("B5:B64").Value

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

End Sub

Mel
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
YOU have added some code to my update

Rich (BB code):
For Each cell In Sheets(1).Range("A4, A6, A8, A10, A12, A14, A16, A18, A20, A22, A24, A26, A28, A30").Value
ComboBox2.AddItem cell.Value
Next

DELETE code shown in BOLD & hopefully, problem solved

Dave
 
Upvote 0
Perfect. Thank you so much.

I've a long way to go and much to learn! I shall now try to figure out how to deal with the rest of this form which has 25 text boxes to "submit" data to up to 60 worksheets.

Mel
 
Upvote 0
Perfect. Thank you so much.

I've a long way to go and much to learn! I shall now try to figure out how to deal with the rest of this form which has 25 text boxes to "submit" data to up to 60 worksheets.

Mel

Your welcome but take this lesson away with you - DO NOT make changes to code offered to you before to you tried them out.

A suggestion for your textboxes to range would be if they are all using their default names "TextBox1" TextBox2" etc
would be to use an array - something like following

VBA Code:
Private Sub CommandButton1_Click()
    Dim arr(1 To 25) As Variant
    Dim NextRow As Long
    Dim i As Integer
    
    For i = 1 To 25
        arr(i) = Me.Controls("TextBox" & i).Value
    Next i
    
     With Worksheets("Sheet1")
        NextRow = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
        .Cells(NextRow, 1).Resize(, UBound(arr)).Value = arr
     End With
End Sub

Dave
 
Upvote 0
Many thanks, Dave. No doubt I shall be able to use some of this but, as you might expect, the 'submitting' of the data is not going to be anything like straightforward! If you like a challenge I'll be happy to share the whole problem with you.

Mel
 
Upvote 0
not a solution, just to give you an idea how you could approach it - do what you can & if get stuck, post to the board, plenty here to help you.

Dave
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,462
Members
449,085
Latest member
ExcelError

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