Dictionary
对象是获取唯一列表的好选择。
Private Sub UserForm_Initialize()
Dim Dic As Object, i, sKey, arr, aList()
Set Dic = CreateObject("scripting.dictionary")
With Sheets("Sheet1")
' load data into array
arr = .Range(.[g2], .Cells(.Rows.Count, 2).End(xlUp))
For i = 1 To UBound(arr)
sKey = Trim(arr(i, 2))
If Not UCase(sKey) = "LUNCH BREAK" Then
If Dic.exists(sKey) Then
If arr(i, 1) >= Dic(sKey)(0) Then
Dic(sKey) = Array(arr(i, 1), Trim(arr(i, 6)))
End If
Else
Dic(sKey) = Array(arr(i, 1), Trim(arr(i, 6)))
End If
End If
Next
End With
ReDim aList(Dic.Count, 2)
' header row
aList(0, 0) = "Date"
aList(0, 1) = "Project ID"
aList(0, 2) = "Status"
i = 1
' transfer data from dict to array
For Each sKey In Dic.keys
aList(i, 0) = Dic(sKey)(0)
aList(i, 1) = sKey
aList(i, 2) = Dic(sKey)(1)
i = i + 1
Next
' populate ListBox
With Me.ListBox1
.ColumnCount = 3
.List = aList
End With
End Sub