Error in Deselecting Listbox Selections

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
When the user presses a commandbutton (exit1_click) on userform 1 (group_1), its supposed to deselect any selections from a listbox (miss_rn) on a second userform (test_nr). When i execute the exit1_click routine, I get an error with the line highlighted in red.

"Could not get the List property. Invalid property array index."

Rich (BB code):
Private Sub exit1_Click()
    'Stop
    Debug.Print Me.Name, "exit1_Click() called"
    If FormIsLoaded("test_mr") Then
        MsgBox "test_mr is open"
        
    '    mbEvents = False
        test_mr.miss_rn.Selected(test_mr.miss_rn.ListIndex) = False
    End If
    If agf = 0 Then
        Unload Me
    Else
        Unload Me
    End If
End Sub

In my testing, test_mr has one selected entry.
The code identifies that the form is open as the msgbox triggers.

Is anyone able to provide any suggestions on what may be causing the error, and what possible resolutions might be?
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
If you test your code to see what the value of test_mr.Miss_rn.ListIndex is, I would guess that it is either not a number or not a valid index number for your listbox. A better way to get 'er done might be looping thru all indexes of the listbox, setting selected to false.

Code:
Private Sub exit1_Click()
[COLOR=#0000ff]Dim i&[/COLOR]

'Stop
Debug.Print Me.Name, "exit1_Click() called"
If FormIsLoaded("test_mr") Then
    MsgBox "test_mr is open"
'    mbEvents = False
   [COLOR=#0000ff] Select Case test_mr.miss_rn.MultiSelect
        Case 0  ' Single Select
            test_mr.miss_rn.ListIndex = -1
        Case 1  ' Multi Select
            With test_mr.miss_rn
                For i = 0 To .ListCount - 1
                    .Selected(i) = False
                Next i
            End With
    End Select[/COLOR]
End If
[COLOR=#0000ff]Unload Me[/COLOR]
End Sub
 
Last edited:
Upvote 0
Thank you CalcSux. I have integrated your suggestion into my code but find I am still having a problem with it's preferred behaviour.

userform test_mr is opened. In listbox miss_rn there is one selection (in this case) available for the user to select.
The user clicks on that selection, firing the listbox (miss_rn) change event code.
This listbox change event code opens up a second userform ... group_1
Rich (BB code):
Private Sub miss_rn_Change()

    Debug.Print mbEvents
    If Not mbEvents Then Exit Sub
    mbEvents = False
    Debug.Print Me.Name, "test_mr.miss_rn_Click() called"
    
    With miss_rn
        Debug.Print Me.Name, "test_mr.miss_rn_Click() ListIndex: " & .ListIndex & " (" & .List(.ListIndex) & ")"
        agf = 1 'referred to from uf2_assess_sched
        group_1.Show
    End With
    mbEvents = True
End Sub

With group_1 open, the user can press commandbutton "EXIT" if he wishes to abort the operation and return to the listbox to make another selection.

Rich (BB code):
Private Sub exit1_Click()
    Dim i As Integer
    'Stop
    Debug.Print Me.Name, "exit1_Click() called"
    If FormIsLoaded("test_mr") Then
        MsgBox "test_mr is open"
        mbEvents = False
        Select Case test_mr.miss_rn.MultiSelect
            Case 0
                test_mr.miss_rn.ListIndex = -1
            Case 1
                With test_mr.miss_rn
                    For i = 0 To .ListCount - 1
                        .Selected(i) = False
                    Next i
                End With
        End Select
    End If

    Unload Me
End Sub

In doing so, I am wanting group_1 to close, returning the user back to test_mr with the previously selected item deselected. The code as it is now does not deselect the previously selected item (it remains highlighted), so the user is unable to trigger the listbox change code again by reselecting. (in case they changed their mind). It may react differently if there was another entry to select in the listbox, but right now, that is not the case. How do I make the single entry in the listbox reselectable?

I added the mbevents=false line to the exit2_click event to suppress the miss_rn change event. If I excluded that, and the miss_rn change event were left to execute, I would get a "Could not get the list property. Invalid property array index." again with the line in red. It's an unnecessary line, but if I omit it, I get an error then when it reaches the line to show group_1 ("Form already displayed; can't show modally"). It is for these reason I think the miss_rn change event needs to be suppressed.
 
Last edited:
Upvote 0
I am still puzzled by this.
I suspect, and with my limited VBA experience it may be unfounded, but in Case 0, there is no code to deselect it ... ie .selected item = false, like what the Case 1 has.

I tried adding (in blue)...
Rich (BB code):
Select Case test_mr.miss_rn.MultiSelect
            Case 0
                test_mr.miss_rn.ListIndex = -1
                test_mr.miss_rn.Selected(test_mr.miss_rn.ListIndex) = False
            Case 1
                With test_mr.miss_rn
                    For i = 0 To .ListCount - 1
                        .Selected(i) = False
                    Next i
                End With
        End Select

... but that line gives me the same error as in my original post. Listindex = -1.

I'm thinking this should be an easy solution, but it's not in my knowledge realm. Anyone care to take a crack at it?
 
Upvote 0

Forum statistics

Threads
1,223,277
Messages
6,171,148
Members
452,382
Latest member
RonChand

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