通過VBA做到這一點的一種方法是循環200次校正,將ID與MATCH函數進行比較以找到它所屬的行,而不是第二個循環(通過20000的第二個循環需要很長時間就像你說的)。
對於下面的子我已將200表複製並粘貼到20000表的列5:7中,您可以很容易地自動完成此部分,或者只是爲代碼的每個部分放入正確的工作表參考。
我也放了一個檢查線,以確保有一個匹配的小表當前的ID,否則它會拋出一個錯誤。您可以在此錯誤捕獲中的END IF前面放置一個ELSE,以突出顯示實際未找到的任何ID。這裏的代碼,希望這種方法可以幫助!
Sub replace_things()
With ActiveSheet
For x = 2 To 200 'Change this to however many is in the small table
cur = .Cells(x, 5) 'Defined cur as ID from small table
aMatch = Application.WorksheetFunction.CountIf(.Range("A:A"), cur) 'Check to see there's a match in large table
If aMatch > 0 Then ' if there's a match then...
theRow = Application.WorksheetFunction.Match(cur, .Range("A:A"), 0) 'get the row number the match is actually on
.Cells(theRow, 3) = .Cells(x, 7) 'when row is found, replace with the relevant value from col7 (col3 of small table)
End If
Next x
End With
End Sub
您可以看看Power Query或Microsoft Query – Slai
不知道爲什麼這會得到一個downvote ..如果您正在投票,請提一下這樣做的原因。對我來說,這看起來像一個完整的問題。 –