refresh listbox

Davers

Well-known Member
Joined
Sep 17, 2002
Messages
1,165
Hi everyone. I have a list box that I use code to set the range it gets its values from. It's basically a list of sheets in the workbook...which can change. When the workbook opens, it gets a list of sheet names and populates a range, then it names that range myRng. I have the list box set to myRng...but it seems I have to delete that property and then put it back in for it to work...does anyone know how to do this through code?

Thanks,

Dave
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
If I'm understanding you correctly, you should change the default value to "Choose page" or something like that. You could then choose the page you want. See if that works.
 
Upvote 0
You don't say which type of listbox you are using.

Rather than use a named range for the source of the listbox
use Additem to add each worksheet name to it.

If it is from the forms toolbar you could use the following code to populate it:

Code:
Sub test()
Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ActiveSheet.ListBoxes(1).AddItem ws.Name
Next

End Sub

If it's from the Control Toolbox you could use the following:

Code:
For Each ws In ActiveWorkbook.Worksheets
       ActiveSheet.ListBox1.AddItem ws.Name
Next

And for one on a userform:

Code:
For Each ws In ActiveWorkbook.Worksheets
       UseForm1.ListBox1.AddItem ws.Name
Next

Obviously change sheet/form/control names as required.
 
Upvote 0
I'm not sure if I explained myself well enough...lets see. I have a named range that is called myRng. A list box has it's "ListFillRange" set to myRng. When I open the workbook....my named range may change from A2:A6 to A2:A7 if I've added a sheet. My list box should now be looking at A2:A7...but it's not, it's still looking at A2:A6. It seems that the property for "ListFillRange" didn't update with the new named range, even though I see it in there.

I hope I've explained that a bit better. Thanks for the help,

Dave (y)
 
Upvote 0
Dave

Have you tried any of my suggestions, they eliminate the need to use a named range.
 
Upvote 0
Hey guys, you keep answering way too fast.... :LOL: I'm feverishly trying them.... :eek: :wink:

I'll update in a sec....

Thanks again!!!

Dave
 
Upvote 0
Hey Norie...I like this one!

Code:
For Each ws In ActiveWorkbook.Worksheets 
       ActiveSheet.ListBox1.AddItem ws.Name 
Next

Thanks for the help everyone...I appreciate it!

Have a good day,

Dave
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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