Private Sub CheckBox1_Click()
'Select\Deselect All all items in ListBox1
Dim i As Long
If Not CheckBox1 Is ActiveControl Then Exit Sub
With ListBox1
For i = 0 To .ListCount - 1
.Selected(i) = CheckBox1.Value
Next i
Sheet1.Range("TheListSelected").ClearContents
If CheckBox1.Value Then Sheet1.Range("TheListSelected").Resize(.ListCount).Value = .List
End With
End Sub
Private Sub ListBox1_Change()
Dim i As Integer
Dim r As Integer
Dim counter As Long
If Not ListBox1 Is ActiveControl Then Exit Sub
r = 0
Sheet1.Cells(5, 3).Resize(65531, 1).ClearContents
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
r = r + 1
Sheet1.Cells(r + 4, 3).Value = .Column(0, i)
counter = counter + 1
' To place selected items on sheet next to their respective list item (also done by CommandButton below)
' Sheet1.Cells(r + 4, 3).Value = .Column(0, i)
' Use if have multi column ListBox
' Sheet1.Cells(i, 2).Value = .Column(1, i)
' Sheet1.Cells(i, 3).Value = .Column(2, i)
' Sheet1.Cells(i, 4).Value = .Column(3, i)
End If
Next i
CheckBox1.Value = counter = .ListCount
End With
End Sub
Private Sub UserForm_Initialize()
Dim DSO As Object
Dim i As Long
Dim counter As Long
With ThisWorkbook.Sheets(1)
' ListBox1.List expects an array > 1 cell; if only 1 cell use AddItem
If .Evaluate("TheListCount") = 1 Then
ListBox1.AddItem (Range("TheList"))
End If
If .Evaluate("TheListCount") > 1 Then
ListBox1.List = Range("TheList").Value
End If
If .Evaluate("TheListCount") = 0 Then
Sheet1.Cells(5, 3).Resize(65531, 1).ClearContents
End If
End With
' Pre loads selected items (no duplicates...however a pivot table can eliminate duplicates)
Set DSO = CreateObject("Scripting.Dictionary")
DSO.CompareMode = vbTextCompare
For i = 0 To ListBox1.ListCount - 1
DSO.Add ListBox1.List(i), i
Next i
ListBox1.ListIndex = -1
For Each cell In Sheet1.Range("TheListSelected")
If DSO.Exists(cell.Text) Then
ListBox1.Selected(DSO(cell.Text)) = True
counter = counter + 1
End If
Next cell
CheckBox1.Value = counter = DSO.Count
Set DSO = Nothing
End Sub
Private Sub CommandButton1_Click()
' Place selected items on sheet next to their respective list item
Dim i As Integer
Dim r As Integer
r = 0
Sheet1.Cells(5, 3).Resize(65531, 1).ClearContents
With ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
r = r + 1
Sheet1.Cells(i + 5, 3).Value = .Column(0, i)
' Use if have multi column ListBox
' Sheet1.Cells(i, 2).Value = .Column(1, i)
' Sheet1.Cells(i, 3).Value = .Column(2, i)
' Sheet1.Cells(i, 4).Value = .Column(3, i)
End If
Next i
End With
End Sub
Bookmarks