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
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Bob

Where is the listbox located?
 
Upvote 0
Bob

How should the population of the listbox be more 'dynamic'?
 
Upvote 0
The list of items is defined on a sheet called 'groups'. At the moment they are all known about, but in the future the list might get longer. I want to write some code that tracks the list of groups, and increases as it gets longer, or in if it get shorter
 
Upvote 0
You can populate the listbox in the userform's Initialize event
VBA Code:
Private Sub UserForm_Initialize()

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

End Sub
 
Upvote 0
Hi Norie,
Thanks for that.
I don't really understand your code, but before I try to see how it works, where exactly should this code be.
Should it be within the main macro? or where.
Please advise.
Thanks
Bob
 
Upvote 0
It would go in the userform module.
 
Upvote 0
1612342332279.png
1612342499265.png
 
Upvote 0
Hi Norie,
Like in the examples above?

I single stepped your code and get an error message:-
Run-time error '381':
Could not set the List Property. Invalid property array index.

Where am going wrong.
Thanks
Bob
 
Upvote 0

Forum statistics

Threads
1,215,436
Messages
6,124,869
Members
449,192
Latest member
MoonDancer

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