userform filling combobox with cell values

austin350s10

Active Member
Joined
Jul 30, 2010
Messages
321
I am working with a userform and trying to load a combobox with values upon initialization. Below is the code i am trying to use which is just supposed to reference a sheet containing the values I want to use. The below code keeps giving me the error: "Run-time error '1004' Application-defined or object-defined error" Any ideas what I'm doing wrong here?

Code:
Private Sub UserForm_Initialize()

Dim ws As Worksheet
Set ws = Sheets("StaffList")


With Sheets("StaffList")
    lastrow = ws.Cells(Rows.Count, 2).End(xlUp).Row
End With



i = 0
With Me.boxNames
    .Clear
    Do
                    .AddItem
                    .List(i, 0) = ws.Cells(i, 1).Value
                    .List(i, 1) = ws.Cells(i, 2).Value
                    i = i + 1
    Loop Until i = lastrow
End With
     
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Its the '.AddItem'
you actually have to add an item... boxNames.additem "myItem"
so
.addItem ws.Cells(i, 1).Value
 
Upvote 0
I still get the same error. I have even tried taking the comobox out of the equation and that does not work either. even something like this does not work:

Code:
myID = ws.Cells(i, 1).Value
 
Upvote 0
Yes. pretty sure I am just referencing the cell wrong. Can't quite remember how to correctly reference the cell value
 
Upvote 0
Try this:-
Code:
Private Sub UserForm_Initialize()
Dim ws As Worksheet
Dim i
Dim lastrow
Set ws = Sheets("StaffList")


With Sheets("StaffList")
    lastrow = ws.Cells(Rows.Count, 2).End(xlUp).Row
End With
i = 1
With Me.Boxnames
    .ColumnCount = 2
    .Clear
    Do
        .AddItem ws.Cells(i, 1).Value
        .List(.ListCount - 1, 1) = ws.Cells(i, 2).Value
        i = i + 1
    Loop Until i = lastrow
.ListIndex = 0
End With
End Sub
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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