Multiple LIST BOX selection

Multiple LIST BOX selection

Tutorial on Multiple LIST BOX selection in VBA. This is an excel VBA tool; this is another type of ‘User form selection.

All of the VBA functions are generic in nature and can be used in other excel vba tools and for learning purpose. Its code module is very useful in day- 2 – day reporting.

 

Multiple LIST BOX selection

 

Code to design tool forĀ  Multiple LIST BOX selection in VBA :

Dim i As Integer, ctr As Integer

Private Sub CheckBox1_Click()

If CheckBox1.Value = True Then

For i = 0 To ListBox1.ListCount - 1

ListBox1.Selected(i) = True

Next i

End If

 

If CheckBox1.Value = False Then

For i = 0 To ListBox1.ListCount - 1

ListBox1.Selected(i) = False

Next i

End If

 

End Sub

 

Private Sub CheckBox2_Click()

If CheckBox2.Value = True Then

For i = 0 To ListBox2.ListCount - 1

ListBox2.Selected(i) = True

Next i

End If

 

If CheckBox2.Value = False Then

For i = 0 To ListBox2.ListCount - 1

ListBox2.Selected(i) = False

Next i

End If

 

End Sub

 

Private Sub CommandButton1_Click()

counter = 0

ctr = ListBox1.ListCount - 1

For i = 0 Toctr

If ListBox1.Selected(i - counter) = True Then

ListBox2.AddItem ListBox1.List(i - counter)

ListBox1.RemoveItem (i - counter)

counter = counter + 1

End If

 

 

Next i

 

 

End Sub

 

Private Sub CommandButton2_Click()

Dim counter As Integer

counter = 0

 

For i = 0 To ListBox2.ListCount - 1

If ListBox2.Selected(i - counter) Then

ListBox1.AddItem ListBox2.List(i - counter)

ListBox2.RemoveItem (i - counter)

 

counter = counter + 1

End If

Next i

 

CheckBox2.Value = False

 

 

End Sub

 

Private Sub OptionButton1_Click()

ListBox1.MultiSelect = 0

ListBox2.MultiSelect = 0

End Sub

 

Private Sub OptionButton2_Click()

ListBox1.MultiSelect = 1

ListBox2.MultiSelect = 1

End Sub

 

Private Sub OptionButton3_Click()

ListBox1.MultiSelect = 2

ListBox2.MultiSelect = 2

End Sub

 

Private Sub UserForm_Initialize()

 

With ListBox1

.AddItem "Sales"

.AddItem "Production"

.AddItem "Logistics"

.AddItem "Human Resources"

End With

 

OptionButton3.Value = True

 

End Sub