Need to call Listbox in a subroutine

manney

New Member
Joined
Mar 25, 2002
Messages
46
I am loading a listbox ("Listbox1") by name in the main body of code. I want to be able to do it in a subprogram, and I cannot seem to get it to work.

Sample code--

'Load Listbox
For iRow = 2 To iListLength + 1
Listbox1.AddItem (Sheets("Sheet1").Range(CallRange).Value)
Next iRow

iListLength is the number of items in the listbox
The listbox items themselves are on a worksheet (Sheet1, CallRange)

How might this code be modified so it can be called as a subprogram? I'd need to pass the name of the listbox as a parameter, plus (of course) sheet and range names.

Thank you,
pmanney@oriuscorp.com
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, you can try something like this. As long as you pass the full range object, you don't need to specify the sheet's name.

<pre>Option Explicit

Private Sub FillLB(LB As MSForms.ListBox, WithRange As Range)
LB.Clear
LB.List = WithRange.Value
End Sub

Sub Test()
FillLB Sheet1.ListBox1, Sheet1.Range("A2:A6")
FillLB Sheet1.ListBox1, Sheet1.Range("B2:B4")
FillLB Sheet1.ListBox2, Sheet2.Range("D2:C9")
FillLB Sheet1.ListBox2, Sheet2.Range("D2:D7")
End Sub</pre>
 
Upvote 0
I won't get a chance to try it until tomorrow-- but thank you for your quick response!

P Manney
 
Upvote 0
Thank you. I substituted "UserForm1" for "Sheet1" and got it to load properly.

This gives me the basic idea: thanks for your help.

Now, I'll try to figure out how to pass a choice to the listbox.

Thanks again,
pmanney@oriuscorp.com
 
Upvote 0

Forum statistics

Threads
1,222,151
Messages
6,164,244
Members
451,882
Latest member
Bigtop

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