应用replace函数的最佳方法
有一张桌子,有两列:
- 话
- 翻译字词
我需要用主表中的第二个单词replace主表中的所有单词。
什么是最好的方法来改变一个巨大的表的所有值?
Sub aizvietot_tulk() Dim ws As Worksheet, tulkojumi As Worksheet Set ws = ThisWorkbook.Worksheets("final_data") Set tulkojumi = ThisWorkbook.Worksheets("Lapa1") Set wsfin = ThisWorkbook.Worksheets("tulkojums") RowCount = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row rowcountt = tulkojumi.Cells(tulkojumi.Rows.Count, "A").End(xlUp).Row colcount = ws.UsedRange.Columns(ws.UsedRange.Columns.Count).Column For i = 3 To RowCount For j = 1 To colcount vertiba = ws.Cells(i, j).Value For k = 1 To rowcountt mainamais = tulkojumi.Cells(k, 1).Value jaunums = tulkojumi.Cells(k, 2).Value nomainits = replace(vertiba, mainamais, jaunums) wsfin.Cells(i, j) = Replace(vertiba, mainamais, jaunums) Next k Next j Next i End Sub
这是我真正快速的鞭打,我相信你可以适应你的。 你只需要一个循环。
Sub test() Dim wsfix As Worksheet Set wsfix = Sheet1 Dim wskey As Worksheet Set wskey = Sheet2 Dim fixlastrow As Integer fixlastrow = wsfix.Cells(Rows.Count, 1).End(xlUp).Row Dim keylastrow As Integer keylastrow = wskey.Cells(Rows.Count, 1).End(xlUp).Row Dim fndit As String Dim repl As String For i = 1 To keylastrow fndit = wskey.Cells(i, 1) repl = wskey.Cells(i, 2) wsfix.Range("A1:B" & fixlastrow).Replace What:=fndit, Replacement:=repl Next End Sub