Check if a Listbox item has been selected

Asked

Viewed 3,790 times

0

I have a list ListBox1 q a check box ChckBox1 in a Userform. I want to activate or inactivate the checkbox (CheckBox1.Enabled = True/False if an item in the list is selected.

The check-box is disabled initially, but if something is selected, I want it to be enabled for marking.

At the moment I am using a loop for validation, but I would like to know if there is a more direct way, without having to analyze item by item.

Private Sub ListBox1_Change()

    Dim s As Boolean: s = False
    For i = 0 To ListBox1.ListCount - 1
        If ListBox1.Selected(i) Then
            s = True
            Exit For
            End If
        Next i
    CheckBox1.Enabled = s

End Sub

1 answer

-1

Use:

Private Sub ListBox1_Change()
Dim s As Boolean: s = False

If ListBox1.ListIndex <> -1 Then
   s = True
End If

CheckBox1.Enable = s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.