Populate a listbox from worksheet range... BUT do it with a BAS module call

gymsheets

New Member
Joined
Feb 22, 2010
Messages
2
Populate a listbox from worksheet range BUT do it with a BAS module call


Greetings-

I have a column of data (really several different columns of data) and I want to throw it into a userform listbox- not too hard.

The tricky part is that I want to be able to throw the code into a *.bas module, generalize it and be able to efficiently refer back to the code like this:


Call populatelb(WhatListbox,StartRange,EndRange)


The big problem is when I attempt to generalize the listbox populating function in a bas, if I dimension a variable as a listbox I *CANNOT* call it from above, I keep getting a type mismatch error. I have tried ****-near everything with no luck.


Below is what works and I would like to generalize it so I can call it as many times as I need to with different ranges and listboxes I specify.


Dim lb As MSForms.listbox


Dim rcArray() As Variant
Dim lrw As Long, lcol As Long
Dim rngTarget As Range

'Define the range you want to use
Set rngTarget = Worksheets("Sheet1").Range("A4:A50")



'Set the boundaries of the array
ReDim Preserve rcArray(1 To rngTarget.Rows.Count, 1 To rngTarget.Columns.Count)

'Fill the array with data from the worksheet
With rngTarget
For lcol = 1 To .Columns.Count
For lrw = 1 To .Rows.Count
rcArray(lrw, lcol) = rngTarget.Cells(lrw, lcol)
Next lrw
Next lcol
End With

'Place the array in the combobox
Set lb = Me.ListBox1
With lb
.List = rcArray
End With


Many, many thanks for your insights
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Try

Code:
Public Sub populatelb( _
    lb As MSForms.ListBox, _
    StartRange as range, _
    EndRange as range)
Dim rcArray As Variant
Dim rngTarget As Range

    'Define the range you want to use
    Set rngTarget = Range(StartRange, EndRange)
    
    'Fill the array with data from the worksheet
    rcArray = rngTarget
    lb.List = rcArray
End Sub
 
Upvote 0
This works like a charm:

Call poplbcombo(ListBox3, "A1:A45")



Module:

Public Function poplbcombo(lbdata As msforms.listbox, b As String)
Dim rMyCell As Range
Set rMyCell = Range(b)


Dim a As String
a = (Worksheets("Sheet1").Range("A3"))


Set r = lbdata
Dim lb As msforms.listbox


Dim rcArray() As Variant
Dim lrw As Long, lcol As Long
Dim rngTarget As Range

'Define the range you want to use
Set rngTarget = rMyCell




'Set the boundaries of the array
ReDim Preserve rcArray(1 To rngTarget.Rows.Count, 1 To rngTarget.Columns.Count)

'Fill the array with data from the worksheet
With rngTarget
For lcol = 1 To .Columns.Count
For lrw = 1 To .Rows.Count
rcArray(lrw, lcol) = rngTarget.Cells(lrw, lcol)
Next lrw
Next lcol
End With

'Place the array in the combobox
Set lb = lbdata
With lb

.List = rcArray
End With


End Function

Cheers, gang
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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