Initialize list box

bob6pp

New Member
Joined
Apr 9, 2010
Messages
13
Hi,
I very new to using boxes etc.
I have been able to create a list box, that takes it's data from a page in the main spreadsheet. The relevant line in the properties is 'rowsource=groups!A2:A15'. I want to make that more dynamic so that I can set it to 'say':- =groups!A2:A20, when the program runs.
From my investigations, I think I need to initialse the list box, and pass the text ie:- '=groups!A2:A20' to it somehow
Please can some show me how to do this, and where the code actually lives?

Thanks
Bob
 
Bob

Remove what you have in RowSource in Properties.

You can do that manually or use the below code to do it.
VBA Code:
Private Sub UserForm_Initialize()

    Me.ListBox1.RowSource = ""

    With Sheets("Groups")
        Me.ListBox1.LIst = .Range("A2", .Range("A" & Rows.Count)).End(xlUp).Value
    End With

End Sub
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Norie,
Sorry found your reply.
I've adapted it:-

Private Sub UserForm_Initialize()

Me.ListBox1.RowSource = "groups!A2:A5"

End Sub

This works just like I want.

This also works:-
Private Sub UserForm_Initialize()

enndd = 10

Me.ListBox1.RowSource = "groups!A2:A" & enndd

End Sub

However, please can you help with the next problem.
How do I pass a variable to this sub-routine, as the 'enndd' (variable) needs to change depending on the list lenght. I can calculate the list length in the main body of the code, and need this value to set the end point of my list.
Hope that makes sense.

I appreciate your time and effort in helping me
Thanks
Bob
 
Upvote 0
Bob

Why not use the code I posted?

It will calculate the last row in the column and populate the listbox accordingly.

No need to pass any variable.:)
 
Upvote 0
Well the reason is that I get an error message:-
Run-time error 381:-
Cannot set the List property. Invalid property array index.
 
Upvote 0
Bob

There's a slight typo in the code I posted, this is how it should be.
VBA Code:
Private Sub UserForm_Initialize()

    Me.ListBox1.RowSource = ""

    With Sheets("Groups")
        Me.ListBox1.List = .Range("A2", .Range("A" & Rows.Count).End(xlUp)).Value
    End With

End Sub
 
Upvote 0
Solution
Hi Norie,
That certainly was a subtle typo, took me quite a while to spot it.

However your code works fine, and does exactly what I want. Many thanks.

Still not too sure how it works however.

Regards
bob
 
Upvote 0

Forum statistics

Threads
1,215,446
Messages
6,124,895
Members
449,194
Latest member
JayEggleton

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