[RESOLVED] List Box "nothing selected" Problem
I have a List Box that works fine except if nothing is selected the entire contents of the list box is automatically selected and imported. I'm looking for my code to display a custom message box if nothing is selected. I can get the message box portion to work fine when I change
If iListCount = -1 Then
frmMsgBox.Show
to
If iListCount = 0 Then
frmMsgBox.Show
but the first row of the list is never recognized and if I dont select anything and cancel the process it will still imports the first row ???
Been doing the trial/error thing and researching doing the for a couple of days with no success. Need the experts...
My Code:
Private Sub cmdStartImport_Click()
Dim iListCount As Integer
Dim iRow As Integer
Dim rStartCell As Range
Application.ScreenUpdating = False
Set rStartCell = Sheets("Log").Range("F1048576").End(xlUp).Offset(1, 0)
For iListCount = ListBox1.ListIndex To ListBox1.ListCount - 1
iRow = iRow + 1
rStartCell.Cells(iRow, 1).Value = ListBox1.List(iListCount, 0)
rStartCell.Cells(iRow, 2).Value = ListBox1.List(iListCount, 1)
rStartCell.Cells(iRow, 3).Value = ListBox1.List(iListCount, 2)
rStartCell.Cells(iRow, 4).Value = ListBox1.List(iListCount, 3)
'Is a row Selected
If iListCount = -1 Then
frmMsgBox.Show
Exit Sub
End If
Next iListCount
Set rStartCell = Nothing
Application.ScreenUpdating = True
End Sub
Thanks for any help!
Re: List Box "nothing selected" Problem
is your listbox set for multiselect?
multi select listboxes do not return -1
testing for 0 will mean that the first item can never be selected
also i looks like you should check if nothing is selected before the for loop, otherwise it could loop from -1 or 0, hence all items
Re: List Box "nothing selected" Problem
The fix was changing the MultiSelect property to Single.
I've worked with it now for a couple of hours now and can't get it to fail...
Thanks alot!!!!