Page 1 of 2 12 LastLast
Results 1 to 10 of 12

ListBox Record Removal Creates Invalid Argument Error

This is a discussion on ListBox Record Removal Creates Invalid Argument Error within the Excel Questions forums, part of the Question Forums category; I have a listbox populated with records. When I highlight a record and press a button next to it, it ...

  1. #1
    Board Regular
    Join Date
    Apr 2010
    Posts
    728

    Default ListBox Record Removal Creates Invalid Argument Error

    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.

  2. #2
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    13,942

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    Try the following line of code instead...

    Code:
    For x = UserForm1.lbxStock2.ListCount - 1 To 0 Step -1
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

  3. #3
    MrExcel MVP
    Moderator
    RoryA's Avatar
    Join Date
    May 2008
    Location
    East Sussex
    Posts
    22,070

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    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

  4. #4
    Board Regular
    Join Date
    Apr 2010
    Posts
    728

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    Perfect! Thanks!!!

  5. #5
    New Member
    Join Date
    Aug 2011
    Posts
    7

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    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

  6. #6
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    13,942

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    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.
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

  7. #7
    New Member
    Join Date
    Aug 2011
    Posts
    7

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    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.

  8. #8
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    13,942

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    I don't know what you mean by a Display object, but shouldn't it be...

    Code:
        If ThisDisplay.ListBox1.Selected(i) Then
            'Your code
        End If
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

  9. #9
    New Member
    Join Date
    Aug 2011
    Posts
    7

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    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
             isSelected = ThisDisplay.ListBox1.Selected(i)
             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

  10. #10
    MrExcel MVP
    Join Date
    Mar 2004
    Location
    Canada
    Posts
    13,942

    Default Re: ListBox Record Removal Creates Invalid Argument Error

    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.
    Domenic Tamburino
    Microsoft MVP - Excel
    xl-central.com - "For Your Microsoft Excel Solutions"

Page 1 of 2 12 LastLast

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


DMCA.com