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.
 
Calitila,

Did you ever figure out how to get the Listbox to work in ProcessBook? I'm working with that same application and having the same issue you're having. I'm going to try using a ListView control instead of the ListBox to see if that works, but it seems like it might be a bug with PI ProcessBook.

Regards,

Greg
 
Upvote 0

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Calitila,

I found a fix for this on PI's vCampus discussion forum. Turns out it's a conflict with a built-in ProcessBook property with the same name "Selected"


"PI ProcessBook extends every symbol with a number of properties. Among these properties is a "Selected" property. This is different then the Listbox's builtin Selected property. This extended property is what's being accessed when the listbox controls are accessed by name in PI ProcessBook (i.e. ListBox1.Selected is the PI-ProcessBook provided extended property).

In order to use the bultin Selected property of the Listbox control, we need to access the Listboxes as native MSForms.ListBox controls. Please try the sample code below:

Dim i As Long
Dim myMessage As String

Dim lst As MSForms.ListBox
Set lst = ListBoxGC

For i = 0 To lst.ListCount - 1
If lst.Selected(i) = True Then
myMessage = myMessage & lst.List(i) & ";" & vbCrLf
End If
Next i

MsgBox myMessage

Steve Pilon, Product Manager at OSIsoft
 
Upvote 0

Forum statistics

Threads
1,214,539
Messages
6,120,100
Members
448,944
Latest member
SarahSomethingExcel100

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