MsgBox if nothing selected in Listbox?

nd1marcello

Board Regular
Joined
Jun 1, 2012
Messages
228
Hi all,

I am trying to create a MsgBox that alerts a user when nothing was selected in ListBox5. It is a multiselect listbox. What do I need to tweak here?

Code:
    If ListBox5.ListCount = -1 Then
        MsgBox "Missing query selection." & vbNewLine & vbNewLine & "Please make a RETAILER selection.", vbOKOnly, "Attention!"
        Exit Sub
    End If
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

I have the exact same issue as this, not surprisingly I'm using almost identical code:

Code:
Private Sub AddLines_Click()

 If ApprovalBox.ListCount = -1 Then
        MsgBox "Please select lines to add.", vbExclamation, "Missing Selection"
        End If
            
End Sub

I've tried with .SelctedItems.Count as well but with no luck.

Any help would be greatly appreciated. Thanks
 
Upvote 0
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

Hi,

I am having the same problem.

I have a list box input populated from a text box and command button. I have also added a delete button for the user to delete selected items in the list box.

The user is prompted by a message box when they attempt to delete an item and the list box is empty. This code works perfectly:

Code:
If LB1.ListCount = 0 Then
            MsgBox "There are no items to delete."
        Exit Sub
       End If

I am trying to also code a message box when the user presses the delete button but no item in the list has been selected. My code as follows, but it is not working:

Code:
If LB1.ListIndex = 0 Then
        MsgBox "There are no items selected to delete"
    Exit Sub
    Else
        LB1.RemoveItem (LB1.ListIndex)
    End If

Can anyone point me in the right direction?
Thanks
S
 
Upvote 0
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

I have also tried the following:

Code:
    If LB1.Value = "" Then         
      MsgBox "There are no items selected to delete"    
     Exit Sub     
     
    Else         
      LB1.RemoveItem (B1.ListIndex)         
      
      'Unselect any items in listbo after delete          
      LB1.Value = ""         
    
      'Cursor defaults back to input text box         
      txbEnter.SetFocus     
    End If

This also does not work?
 
Upvote 0
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

Hi,

I ended up doing the following, which worked. I would however still like to know what I should have done in the previous codes if someone knows:

Code:
    'If there are items polulated in the listbox
    If LB1.Value = LB1 Then

        'remove the selected item
        L1.RemoveItem (LB1.ListIndex)
        
        'unselect any other remaining items
        LB1.Value = ""

        'return cursor to the input textbox
        txbEnter.SetFocus

    Exit Sub
    
    End If
    
    'if the listbox is empty
    If LB1.ListCount = 0 Then
    
        MsgBox "There are no items to delete."
    
    Else

        'remaining option - listbox is populated but nothing selected
        MsgBox "You have not selected and item to be deleted"


    End If
 
Upvote 0
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

ListIndex doesn't really work with multiselect listboxes and ListCount will tell you the no of items in the listbox.

To check if nothing has been selected in a multiselect listbox you need to loop through the list.
Code:
Dim boolSelected As Boolean 

    With ListBox1
        For I = 0 To. ListCount - 1 
            boolSelected = boolSelected And. Selected(I)
        Next I
    End With
    
    If Not boolSelected Then
          MsgBox "Nothing selected in listbox."   
    End If
If nothing is selected in a single select listbox it's ListIndex will be -1.
 
Last edited:
Upvote 0
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

Ah, brilliant!

Thanks Norie!

This worked:

Code:
    If LB1.ListIndex = -1 Then
        MsgBox "There are no items selected to delete"
 
Upvote 0
Re: MsgBox if nothing selected in Listbox? CODE PROVIDED

I realize that this is a very old thread, but came across it at the top of a search today and was tripped up by a logic error. Shouldn't the "And" in the loop code below be an "Or". As it stands, I think that the boolean variable would only end up true if EVERY item were selected, so the message box fires unless that is the case.

Richard T

ListIndex doesn't really work with multiselect listboxes and ListCount will tell you the no of items in the listbox.

To check if nothing has been selected in a multiselect listbox you need to loop through the list.
Code:
Dim boolSelected As Boolean 

    With ListBox1
        For I = 0 To. ListCount - 1 
            boolSelected = boolSelected And. Selected(I)
        Next I
    End With
    
    If Not boolSelected Then
          MsgBox "Nothing selected in listbox."   
    End If
If nothing is selected in a single select listbox it's ListIndex will be -1.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,190
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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