如何使工作表函数更高效?
我试图做一个工作表函数,循环行更有效率。 这是我所拥有的 –
Dim ws as worksheet, wks as worksheet, lastrow as long, i as long Set ws = Sheets("Sheet1") Set wks = Sheets("Sheet2") With ws Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row For i = 2 to Lastrow .Range("CS" & i) = Application.Vlookup(ws.Range("B" & i), wks.Range("B2:X100000"),23,false) next end with
谢谢!
我将删除循环中所有的单元格的读写操作:
Dim rg_table As Range, rg_lookup As Range, values(), r& Set rg_table = [Sheet1].UsedRange Set rg_lookup = [Sheet2!B2:X100000] ' load the values to search values = rg_table.columns("B").Value ' lookup the values For r = 2 To UBound(values) values(r, 1) = Application.VLookup(values(r, 1), rg_lookup, 23, False) Next ' write the results to the sheet rg_table.columns("CS") = values
您应该尽可能避免遍历行。 我build议你在CS列的范围内粘贴Vlookup函数。
这是如果你使用find
function将如何去:
Dim ws as worksheet, wks as worksheet, lastrow as long, i as long Set ws = Sheets("Sheet1") Set wks = Sheets("Sheet2") Lastrow = ws.Range("A" & Rows.Count).End(xlUp).Row For i = 2 to Lastrow ws.Range("CS" & i) = wks.Range("B:B").find(ws.Range("B" & i).Text, , xlValues).Offset(0, 22).Value Next i
让我知道如果它的作品。