MrExcel Publishing
Your One Stop for Excel Tips & Solutions

Creating listboxes in VBA


Posted by Chris M. on November 14, 2001 5:15 PM


Can someone just give me a sample code that creates a list box and then lts me put the information from this code into it. I tried using the help files to get some examples. All I got was

With Worksheets(1)
Set lb = .Shapes.AddFormControl(xlListBox, 100, 10, 100, 100)
For x = 1 To 10
lb.ControlFormat.AddItem x
Next
End With

But i'm not sure how to create an object that is a listbox so I can refer the code to it. I'm just begging in the VBA environment so please go easy on me.

I'm trying to get it into this, so I can use it. THis code was offered by someone on this site which I am very gratefull for. I just need someone to show me how to use it. Thanks.

Sub List_Files()


With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = CurDir()
.SearchSubFolders = True 'or false
.Execute
For i = 1 To .FoundFiles.Count
something.AddItem.FoundFiles (i) 'where something is some listbox somewhere
Next
End With

End Sub


Posted by Ivan F Moala on November 15, 2001 1:09 AM

Try
Sub List_Files()
Dim Lb
Dim i As Integer

With Worksheets(1)
Set Lb = .Shapes.AddFormControl(xlListBox, 100, 10, 100, 100)
End With
With Application.FileSearch
.NewSearch
.FileType = msoFileTypeAllFiles
.LookIn = CurDir()
.SearchSubFolders = True 'or false
.Execute
For i = 1 To .FoundFiles.Count
Lb.ControlFormat.AddItem .FoundFiles(i) 'where something is some listbox somewhere
Next
End With

End Sub

Ivan

Posted by Chris M. on November 15, 2001 10:27 AM

Thank you so mutch.