ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with multiselect listbox items selection (https://www.excelbanter.com/excel-programming/398838-problem-multiselect-listbox-items-selection.html)

[email protected]

Problem with multiselect listbox items selection
 
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


Chip Pearson

Problem with multiselect listbox items selection
 
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)

wrote in message
oups.com...
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



[email protected]

Problem with multiselect listbox items selection
 
On 6 Okt, 22:10, "Chip Pearson" wrote:
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)

wrote in message

oups.com...



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- Skjul sitert tekst -


- Vis sitert tekst -


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

Frank



All times are GMT +1. The time now is 02:58 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com