这是另一个选项,这次使用字典(添加对 Microsoft Scripting Runtime 的引用,它还有其他几个非常有用的对象 - 不要在没有它的情况下开始 VBA 编码!)
正如所写,输出未排序 - 这可能有点令人讨厌。无论如何,这里有几个不错的小技巧:
Option Explicit
Public Sub OutputLists()
Dim list1, list2
Dim dict1 As Dictionary, dict2 As Dictionary
Dim ky
Dim cel As Range
Set dict1 = DictionaryFromArray(Array("a", "b", "c", "e"))
Set dict2 = DictionaryFromArray(Array("b", "e", "c", "d"))
Set cel = ActiveSheet.Range("A1")
For Each ky In dict1.Keys
PutRow cel, ky, True, dict2.Exists(ky)
If dict2.Exists(ky) Then
dict2.Remove ky
End If
Set cel = cel.Offset(1, 0)
Next
For Each ky In dict2
PutRow cel, ky, False, True
Set cel = cel.Offset(1, 0)
Next
End Sub
Private Sub PutRow(cel As Range, val As Variant, in1 As Boolean, in2 As Boolean)
Dim arr(1 To 2)
If in1 Then arr(1) = val
If in2 Then arr(2) = val
cel.Resize(1, 2) = arr
End Sub
Private Function DictionaryFromArray(arr) As Dictionary
Dim val
Set DictionaryFromArray = New Dictionary
For Each val In arr
DictionaryFromArray.Add val, Nothing
Next
End Function