Results 1 to 3 of 3

Thread: [RESOLVED] List Box "nothing selected" Problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    18

    Resolved [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!

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    18

    Resolved 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!!!!

Posting Permissions

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



Click Here to Expand Forum to Full Width