不要使用物件作為KEY,應該使用簡單型別,判斷存在的方法是Exists,最好使用賦值語句代替.Add,可以避免重複導致的問題。修改後的程式碼:Option ExplicitSub dic() Dim d, i%, j%, temp Dim k, v Set d = CreateObject("Scripting.Dictionary") For i = 1 To Sheet1.[a66].End(3).Row k = Trim(Sheet1.Cells(i, 1)) v = Sheet1.Cells(i, 2).Value d(k) = v Next i For j = 1 To Sheet2.[a66].End(3).Row k = Trim(Sheet2.Cells(j, 1)) If d.Exists(k) Then Sheet2.Cells(j, 2).Value = d(k) End If NextEnd Sub
不要使用物件作為KEY,應該使用簡單型別,判斷存在的方法是Exists,最好使用賦值語句代替.Add,可以避免重複導致的問題。修改後的程式碼:Option ExplicitSub dic() Dim d, i%, j%, temp Dim k, v Set d = CreateObject("Scripting.Dictionary") For i = 1 To Sheet1.[a66].End(3).Row k = Trim(Sheet1.Cells(i, 1)) v = Sheet1.Cells(i, 2).Value d(k) = v Next i For j = 1 To Sheet2.[a66].End(3).Row k = Trim(Sheet2.Cells(j, 1)) If d.Exists(k) Then Sheet2.Cells(j, 2).Value = d(k) End If NextEnd Sub