plz plz help me out with this

A

ayan

I have tried lots of options to populate a combobox from a range but
it just wont happen I dont know why . Am at my wits end. This is one
of the options I tried

Private Sub UserForm_Initialize()
Dim emp As Range
emp =
Workbooks("test.xls").Worksheets("Sheet2").Range("A2").End(xlDown)
For Each Cell In emp
If Cell.Value <> "" Then
ComboBox1.AddItem Cell.Value
End If
Next
ComboBox1.Value = "Please Select"
End Sub

Can someone please please help me out. I dont know what to do.
Tried rowsource, list everything. Nothing works.
 
J

JMay

You need to use Set before emp;
and (Watch for word-wrap below)

Private Sub UserForm_Initialize()
Dim emp As Range
Set emp = Worksheets("Sheet1").Range(Cells(2, 1),
Cells(Range("A2").End(xlDown).Row, 1))
For Each Cell In emp
If Cell.Value <> "" Then
ComboBox1.AddItem Cell.Value
End If
Next
'ComboBox1.Value = "Please Select"
End Sub
 
J

JMay

WATCH OU !! - I omitted your wb reference and I changed your sheetname -
You need to change back to your specs,,
 
A

ayan

You need to use Set before emp;
and (Watch for word-wrap below)

Private Sub UserForm_Initialize()
Dim emp As Range
Set emp = Worksheets("Sheet1").Range(Cells(2, 1),
Cells(Range("A2").End(xlDown).Row, 1))
For Each Cell In emp
If Cell.Value <> "" Then
ComboBox1.AddItem Cell.Value
End If
Next
'ComboBox1.Value = "Please Select"
End Sub








- Show quoted text -



Thanks a lot Jmay, the code works perfectly. You just saved me, I am
doing my internship & have to get a sw working & couldn't get past the
first hurdle.

If its not too much trouble could you explain this line in your code

Set emp = Worksheets("Sheet1").Range(Cells(2, 1),
Cells(Range("A2").End(xlDown).Row, 1))

I understand about the Set part but dont understand why Cells have to
be referenced like that. This might help me in my programming & wont
require me to ask your help again & again. Whats wrong with the way I
used the Cells thing, it took only the last row of info from my range
and the code for that was from a book??

Thanks a lot again, this really is a great community & u ppl are
awesome
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top