Add Items to listbox

MelG

New Member
Joined
Jul 28, 2011
Messages
21
The code below is supposed to take the filenames from all open excel files and add them to the listbox. I keep getting the error "Object doesnt support this property or method". I can confirm List Box 2 definity exists within Sheet 2 of the workbook. Can anyone help?

Code:
Sub MyFileNameArray()
    Dim wb As Workbook
    Dim wbs As Workbooks
    Set wbs = Application.Workbooks
    For Each wb In wbs
        Worksheets("Sheet2").ListBox2.AddItem wb.Name
    Next wb
End Sub
 
Last edited by a moderator:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Not sure why my post has bunched up all the code. 2nd attempt to post the code in legible format.Sub MyFileNameArray() Dim wb As Workbook Dim wbs As WorkbooksSet wbs = Application.Workbooks For Each wb In wbs Worksheets("Sheet2").ListBox2.AddItem wb.Name Next wb End Sub
 
Upvote 0
['code]
Post you code (Here), between square brackets as show
[/code]

FYI:- Your code worked for me !!
 
Last edited:
Upvote 0
Mel, I edited your post. Please surround your code with the CODE tags. When posting (not using Quick Reply) look for the pound sign (#) with your code selected. :)
 
Upvote 0
Thanks guys. I tried replicating in a new workbook and still no luck. Mick G, can you confirm the method used to create you listbox? Just trying to figure out why it worked for you. I created the listbox using the Forms toolbar in excel.
 
Upvote 0
That could be it, you need "ActiveX" !!!
If you use "Forms" you need something Like this:-
Code:
Sub MyFileNameArray()
Dim wb As Workbook
Dim wbs As Workbooks
Set wbs = Application.Workbooks
For Each wb In wbs
    Worksheets("Sheet2").Shapes("List Box 1").ControlFormat.AddItem wb.Name
Next wb
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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