|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
嘿嘿
Sub 替换()
Set ws1 = ThisWorkbook.Sheets("Sheet1")
Set ws2 = ThisWorkbook.Sheets("Sheet2")
Set dict = CreateObject("Scripting.Dictionary")
lastRowWs1 = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
For i = 2 To lastRowWs1
dict(ws1.Cells(i, "A").Value) = ws1.Cells(i, "B").Value
Next i
lastRowWs2 = ws2.Cells(ws2.Rows.Count, "D").End(xlUp).Row
lastColWs2 = ws2.Cells(2, ws2.Columns.Count).End(xlToLeft).Column
Data = ws2.Range(ws2.Cells(2, 4), ws2.Cells(lastRowWs2, lastColWs2)).Value
For i = 1 To UBound(Data, 1)
For j = 1 To UBound(Data, 2)
If dict.exists(Data(i, j)) Then
Data(i, j) = dict(Data(i, j))
End If
Next j
Next i
ws2.Range(ws2.Cells(2, 4), ws2.Cells(lastRowWs2, lastColWs2)).Value = Data
MsgBox "替换完成!"
End Sub
|
|