Problem with multiselect listbox items selection

F

fokrogh

Hi,
I try to use a multiselection listbox, but receive the error message
"Could not get the Selection property. Invalid argument". Any
suggestion why?

Thank you in advance

Frank
_____________________

Dim x As Integer

Private Sub CommandButton1_Click()
ListBox2.Clear
For x = 0 To 9
If ListBox1.Selected(x) = True Then ListBox2.AddItem
ListBox1.List(x)
Next x
End Sub

Private Sub UserForm_Initialize()
Dim myarray()
Dim fs As Object
Dim tekst As String
With UserForm1
.ListBox1.MultiSelect = fmMultiSelectExtended
.CommandButton1.Caption = "Show selections"
.CommandButton1.AutoSize = True
.ListBox2.Clear
Set fs = Application.FileSearch
With fs
.LookIn = "c:\My files\"
.SearchSubFolders = True
.Filename = "*.xls"
If .Execute > 0 Then
ReDim myarray(fs.FoundFiles.Count)
For x = 1 To .FoundFiles.Count
myarray(x) = .FoundFiles(x)
Next x
Else
MsgBox "No files found!", vbInformation, "Empty folder"
End If
For x = 1 To fs.FoundFiles.Count
pos1 = InStrRev(myarray(x), "\", , vbTextCompare)
tekst = Mid(myarray(x), 1, pos1 - 1)
UserForm1.ListBox1.AddItem tekst
Next x
End With
End With
End Sub
 
C

Chip Pearson

Are you sure that you have (at least) 10 elements in the List? A better way
would be to change your code to

For x = 0 To Me.ListBox1.ListCount -1
If Me.ListBox1.Selected(x) = True Then ......

With this code, you don't have to worry about the number of elements in the
list. It is determined at run time by ListCount -1.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
F

fokrogh

Are you sure that you have (at least) 10 elements in the List? A better way
would be to change your code to

For x = 0 To Me.ListBox1.ListCount -1
If Me.ListBox1.Selected(x) = True Then ......

With this code, you don't have to worry about the number of elements in the
list. It is determined at run time by ListCount -1.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consultingwww.cpearson.com
(email on the web site)









- Vis sitert tekst -

Thank you. I just forgot to modify the If-test in the example I found
in vba help.

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top