ListBox Record Removal Creates Invalid Argument Error

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I have a listbox populated with records. When I highlight a record and press a button next to it, it runs the following code which removes the selected record:

Code:
Dim x As Integer
    
    For x = 0 To UserForm1.lbxStock2.ListCount - 1
        If UserForm1.lbxStock2.Selected(x) Then
            UserForm1.lbxStock2.RemoveItem (x)
        End If
    Next x
    If UserForm1.lbxStock2.ListCount = 0 Then
        UserForm1.lbxStock2.BackColor = vbWindowBackground
    End If

The problem is, if the user selects a record other than the last one in the list, they (I) get the error: "Could not get the Selected Property. Invalid Argument."

Can anyone suggest any code modifications that will allow the user to remove whichever record they select, regardless of order?

Thanks.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try the following line of code instead...

Code:
For x = UserForm1.lbxStock2.ListCount - 1 To 0 Step -1
 
Upvote 0
You need to loop backwards:
Code:
    For x = UserForm1.lbxStock2.ListCount - 1 to 0 Step -1
        If UserForm1.lbxStock2.Selected(x) Then
            UserForm1.lbxStock2.RemoveItem x
        End If
    Next x
 
Upvote 0
Hi, I am having a similar error...
I'm simply trying to figure out which items in a listbox are selected (multiselectmulti), but I am getting a compile error...

here is the code:
Sub CommandButton2_Click()
Dim i As Integer
Dim isSelected As Boolean
For i = ThisDisplay.ListBox1.ListCount - 1 To 0 Step -1
isSelected = ThisDisplay.ListBox1.Selected(i)
Next i
End Sub

isSelected = ThisDisplay.ListBox1.Selected(i) - this line gives me the compile error

changing it to:
isSelected = ThisDisplay.ListBox1.Selected
removes the compile error, but don't alow me to loop through to check each item in the listbox.

The error thrown is:
Compile Error:
Wrong number of arguments or invalid property assignment


PS- I am programming in the vb environment in PI processbook.
This same code has worked fine in excel vba.

Thanks for your help
 
Upvote 0
Sub CommandButton2_Click()
Dim i As Integer
Dim isSelected As Boolean
For i = ThisDisplay.ListBox1.ListCount - 1 To 0 Step -1
isSelected = ThisDisplay.ListBox1.Selected(i)
Next i
End Sub

As it stands, and assuming that "ThisDisplay" refers to a UserForm, the above code in Excel VBA should not generate an error. Also, it's not clear as to what it is you're trying to do.
 
Upvote 0
ThisDisplay refers to a Display object in PI Processbook; and it contains the listbox.

as to what I am trying to do-
that wasn't the full code, I left out the lines:
if isSelected then
//code yet to be writen
end if

it is interesting that the listbox works perfectly on single select, but doesn't work on multi select (or extended).
At this point I'm getting by with about 20 checkboxes :( and I'm debating whether it would be worth it to have two single select listboxes that pass the strings between them (selecting one from listbox 1 and pressing a button would remove it from listbox 1 and add it to listbox 2, etc)

Thank you for your help, I appreciate it.
 
Upvote 0
I don't know what you mean by a Display object, but shouldn't it be...

Code:
[font=Verdana]    [color=darkblue]If[/color] ThisDisplay.ListBox1.Selected(i) [color=darkblue]Then[/color]
        [color=green]'Your code[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color][/font]
 
Upvote 0
The code would be:
Code:
Sub CommandButton2_Click()
    Dim i As Integer
    Dim isSelected As Boolean
    For i = ThisDisplay.ListBox1.ListCount - 1 To 0 Step -1
         [COLOR=red]isSelected = ThisDisplay.ListBox1.Selected(i)[/COLOR]
         if isSelcted then
              ' more code
         end if
    Next i
End Sub

the error I recieve is on the red line. if I remove what is inside the for cycle, I get no error, so I'm not having problems getting ahold of Listbox1, the problem is figuring out which of its items are selected or not.
isSelected = ThisDisplay.ListBox1.Selected returns false
isSelected = ThisDisplay.ListBox1.Selected(i) gives me the following error-
Compile Error: Wrong number of arguments or invalid property assignment

The listbox multiSelect property is set to 1 - fmMultiSelectMulti
I get the same error if I change that property to 2 - fmMultiSelectExtended

Thanks again for your patience and help :)
 
Upvote 0
Again, in Excel VBA, it shouldn't generate an error. I tested it and it works fine. Unfortunately, I can't help you when it comes to VB, as apposed to VBA. However, there are others here who should be able to help.
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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