Dim d, A, i '定義變量
Set d = CreateObject("scripting.dictionary") '創建字典D
A = Sheet2.Range("a1").CurrentRegion '將SHEET2當前單元格區域裝入數據A
For i = 1 To UBound(A) '循環數據A,UBOUND(A)=數據A的最大值
d(A(i, 1)) = A(i, 2) '將數據A的第1列裝入字典D作為關鍵字,數據A的第2列裝入字典作為項目
Next i
A = Sheet1.Range("A1:B" & Sheet1.Range("a65536").End(xlUp).Row) 'SHEET1的A:B列裝入數組A
For i = 1 To UBound(A) '循環數組A
If d.exists(A(i, 1)) Then A(i, 2) = d(A(i, 1)) ‘如果數組A第1列存在字典關鍵字,則將字典D裝入數組A第2列
Next i
[a1].Resize(UBound(A), UBound(A, 2)) = A ‘將數組A裝入表中
End Sub