Checking If a listbox has a row selected

Pacman52

Active Member
Joined
Jan 29, 2009
Messages
319
Office Version
  1. 365
Platform
  1. Windows
Hi could someone help me on this one please.. So, I have a listbox on a Userform. On the form I also have a Delete button which is disable when the form loads.

I'm trying to code the button to enable only if there is a row selected. I did give the following a try but no joy sadly.

VBA Code:
If IsNull (listboxEvents) Then
MsgBox "Nothing Selected"
Else....

End if

thanks for any replies Paul
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try using the ListBox's ListIndex property... it will be -1 if nothing is selected, otherwise it will be 0 to .ListCount-1
 
Upvote 0
Hi Rick thanks for the reply - sorry but I'm not understanding what you mean?

Paul
 
Upvote 0
just a follow up to say I worked it out using the code below - not sure if its the best method or not but it seems to work ok.
VBA Code:
   If listboxEvents.ListIndex = 0 Then
    
    cmbDel.Enabled = True

    End If
 
Upvote 0
just a follow up to say I worked it out using the code below - not sure if its the best method or not but it seems to work ok.
VBA Code:
   If listboxEvents.ListIndex = 0 Then
   
    cmbDel.Enabled = True

    End If
If the ListIndex = 0, that means the first item in the list is selected. If ListIndex = 1, that means the second item in the list is selected and so on up to a value equal to ListCount - 1 which would mean the last item in the list is selected. When no item in the list is selected, ListIndex = -1. I am not sure if your code, as written, will work if someone select an item other than the first item... whether it works or not might depend on other code you have associated with this. Anyway, with that said, this modification of your posted first line should work no matter what...

=IF listboxEvents.ListIndex > -1 Then
 
Upvote 0
Hi Rick sorry for delay in responding - Thanks so much for the explanation it always helps knowing / understanding how something works. To be honest I think I was over thinking the issue as in reality there would never be nothing in the list once the end user starts to fill in the forms, that said I have changed the line of code as you kindly wrote just to cover all options.

Many thanks Paul
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

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