Problem identifying if no items selceted in Listbox

pete48

New Member
Joined
May 13, 2020
Messages
6
Office Version
  1. 2016
Platform
  1. Windows
I have a listbox enabled for me to select as many items in the list as I need (MultiSelect = 1). I then have various button that cary our diferent tasks on the selected items in the listbox.

If any of these buttons are clicked I now want to return a message box saying "No Team selected" if no items in the list are selected.

This almost works: If ListBox1Table.ListIndex = 0 Then MsgBox "No Team selected".

The only problem is for the first item in the list being 0. If this is the only item in the list selected I still get the message box "No Team selected"!

Am I on the right lines with my coding or do I need to do something totally different to overcome this problem?

Many thanks
Pete
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
For a multiselect listbox, you need to loop through the listbox like
VBA Code:
Private Sub CommandButton1_Click()
   Dim i As Long
   Dim Flg As Boolean
   
   With Me.ListBox1
      For i = 0 To .ListCount - 1
         If .Selected(i) Then
            Flg = True
            Exit For
         End If
      Next i
   End With
   If Flg Then MsgBox "ok" Else MsgBox "Not ok"
End Sub
 
Upvote 0
For a multiselect listbox, you need to loop through the listbox like
VBA Code:
Private Sub CommandButton1_Click()
   Dim i As Long
   Dim Flg As Boolean
  
   With Me.ListBox1
      For i = 0 To .ListCount - 1
         If .Selected(i) Then
            Flg = True
            Exit For
         End If
      Next i
   End With
   If Flg Then MsgBox "ok" Else MsgBox "Not ok"
End Sub

Works like a dream, thank you Fluff
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,871
Members
449,055
Latest member
excelhelp12345

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