Setting Userform Listbox Dynamic range

simora

Board Regular
Joined
May 7, 2005
Messages
199
I am setting a listbox range based on the selection of a ComboBox.Value using Select case.
Not sure if this is the most efficient way to go about this, but when I set the case statement hard coded like this

Code:
        Select Case ComboBox1.Value
               Case "Sheet1"
                    ListBox1.RowSource = "Sheet1!A4:A200"

It works, however, I am trying to set a dynamic range like this
Code:
        Select Case ComboBox1.Value
              Case "Sheet2"    
                     ListBox1.RowSource = Sheet2.Range("A4", Range("A65536").End(xlUp))

and I get an error in setting the Range.

Can someone please correct my dynamic range code, and also let me know if there's a better way to populate my ListBox from the ComboBox.

Its the same Row source for every page except for the Sheet number, but COmboBox1.Value Selection sheet 1 sets the Row Source to Sheet1.Range("A4", Range("A65536").End(xlUp))
Sheet 2 follows the same pattern etc...

Code and suggestions appreciated.

Thanks
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Not tested, but RowSource is looking for a string like in your first one. To dynamically assign this, you want something like:
Code:
ListBox1.RowSource = Sheet2.Name & "!" & Sheet2.Range("A4", Range("A65536").End(xlUp)).Address

Mark
 
Upvote 0
Perhaps:

Code:
ListBox1.RowSource = Sheets("Sheet2").Range([A4], Cells(Rows.Count,"A").End(xlUp))

HTH,
 
Upvote 0
Smitty:

Your code generated this error:

Application Defined OR Object Defined Error

Seems like it should've worked.
 
Upvote 0
Code:
ListBox1.RowSource = Range(Sheet1.Range("A4"), Sheet1.Range("A65536").End(xlUp)).Address(,,,True)
 
Upvote 0
Thanks GTO:

This is the error that I get with your code.

Object Worksheet failed


Sorry - I plopped that in there a bit quick and hadn't caught the lack of qualifying.
Rich (BB code):
    ListBox1.RowSource = Sheet2.Name & "!" & Sheet2.Range("A4", Range("A65536").End(xlUp)).Address

My first one would run, but only if Sheet2 is active (not my intent, that is the part I missed)

As you can see with Mike's at #6, he catches this. Here is like Mike did, but I think should self-adjust for 2007.
Rich (BB code):
    With Sheet2
        ListBox1.RowSource = Range(.Range("A4"), .Cells(Rows.Count, "A").End(xlUp)).Address(, , , True)
    End With
@Mike:

Cool! I sorta knew it was there, but haven't really used it (external ref). Thanks :)

Mark
 
Upvote 0
Hi Thanks:

Both code posted by mikerickson & GTO, Mike & Mark worked perfectly.

Again Thanks!
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,039
Members
449,063
Latest member
ak94

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