Dim d, arr, i&, k&‘定義變量
Set d = CreateObject("scripting.dictionary")’創建字典
k = Cells(Rows.Count, 4).End(3).Row - 1‘變量賦值
arr = Cells(2, 4).Resize(k, 3).Value’數組賦值
For i = 1 To k‘1到k循環
If arr(i, 1) <> "" Then’如果不為空
If d.exists(arr(i, 1)) Then‘如果arr(i,1)在字典中存在
d(arr(i, 1)) = d(arr(i, 1)) + arr(i, 3)’arr(i,1)對應的結果加上arr(i,3)
Else: d.Add arr(i, 1), arr(i, 3)'創建arr(i,1)為字典key值,同時把arr(i,3)作為字典的item值
End If
End If
Next i
k = Cells(Rows.Count, 1).End(3).Row - 1
arr = Cells(2, 1).Resize(k, 2).Value
For i = 1 To k‘這裏基本和前面壹樣了
If d.exists(arr(i, 1)) Then
If d(arr(i, 1)) = arr(i, 2) Then
d.Remove arr(i, 1)’滿足條件後取消字典中key值為arr(i,1)的內容
End If
End If
Next i
MsgBox IIf(d.Count > 0, "有如下發票號金額不正確:" _
& Join(d.keys, ","), "發票金額均正確")
Set d = Nothing
End Sub