Listbox in form

P

PA

Hi,

Trying to get a listbox to work in a form... but not quite there yet!

The list should read the info from a range, but it empty... The code I
am using is below

Private Sub clients_select_Initialize()
Dim rng As Range, cel As Range
Set rng = ActiveWorkbook.Sheets("clients").Range("b2:b200")
With Me.clients_select_list
.Clear
For Each cel In rng.Cells
.AddItem cel.Value
Next
End With
End Sub

thanks a million
 
J

JP

I'm trying to test your code, but what is the name of the form and
what is the name if the listbox?

--JP
 
P

PA

Hi thanks for your (very) quick reply!!!

My form is "clients_select" and the list is "clients_select_list"

Thanks-

PA
 
R

Rick Rothstein

You should not try to type the procedure headers in long hand; rather, use
the dropdown box on the left side of the code window to pick the object and
use the dropdown box on the right side of the code window to pick the event
procedure. If you would have done that, you would have found that the proper
header for the UserForm's Initialize event was not this...

Private Sub clients_select_Initialize()

but, rather, that it was this instead...

Private Sub UserForm_Initialize()

Also, this part of the code (the header and footer) would not have required
you to type anything (especially useful for events having long headers such
as MouseDown).
 
R

Rick Rothstein

No, you should not use parentheses here nor anywhere else where they are not
required by syntax. While it would work here without problem, doing so can
get you in trouble in other situations (by either generating errors for
calls requiring multiple arguments or generating incorrect results in ByRef
arguments used to pass values from the called code back into the calling
code).
 
J

JP

The correct code should be:

Private Sub UserForm_Initialize()
Dim rng As Range, cel As Range
Set rng = ActiveWorkbook.Sheets("clients").Range("b2:b200")
With Me.clients_select_list
.Clear
For Each cel In rng.Cells
.AddItem cel.Value
Next
End With
End Sub

You should review Rick's post -- never try to type out the names of
objects on your form, they are all available from the dropdowns at the
top of the code box.

--JP
 

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