Fred
Fred

Reputation: 121

data type of Listbox Index

When I search information about Listbox, I see that most people use Variant as data type for the index number of the list. Example:

Dim varItm As Variant
If Me.myListBox.ItemData.Selected(varItm) = True 

Is it better to use Variant or Integer as data type?

Upvotes: 3

Views: 914

Answers (1)

Chrismas007
Chrismas007

Reputation: 6105

You are confusing two things. When you have a multi-selection ListBox, users will use the following code because .SelectedItems collection has to be variant:

Dim varItm as Variant
For Each varItm in myListBox.SelectedItems
    'Do Something
Next varItm

However, there is further documentation on myListBox.Sected(x) where x is supposed to be Long but really could be any number containing variable. Variant allows for numbers to be stored, so it would work, but better to define x as explicitly containing a number (Integer or Long) More on Integer vs. Long.

Dim x As Integer
For x = 0 to myListBox.ListCount - 1
    If myListBox.Selected(x) Then
        'Do Something
    End If
Next x

Upvotes: 2

Related Questions