比较两个数组除去项的问题
设有数组a()和b(),如果a()中的值可以在b()找到,则去掉该a()。
[解决办法]
Dim col As New CollectionDim i As LongDim j As LongDim have As BooleanFor i = 0 To UBound(a) have = False For j = 0 To UBound(b) If a(i) = b(j) Then have = True : Exit For Next If Not(have) Then col.Add(a(i))NextRedim a(0 To col.Count - 1)For i = 0 To UBound(a) a(i) = col(i)Next
[解决办法]
楼主的需求是保持 a() 中的元素不存在于 b() 中吗?
可以借助一个不可见的 ListBox 来缓存:
For i = LBound(a) To UBound(a) For j = LBound(b) To UBound(b) If a(i) = b(j) Then Exit For Next j If j > UBound(b) Then List1.AddItem a(i)Next iRedim a(LBound(a) + List1.ListCount - 1)For i = 0 To List1.ListCount - 1 a(i + LBound(a)) = List1.List(i)Next i