我正在尝试创建一个更有效循环遍历行的工作表函数。这就是我所拥有的 -
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
谢谢!
答案 0 :(得分:1)
您应该尽可能避免遍历行。我建议您在CS列的范围内粘贴Vlookup函数。
答案 1 :(得分:1)
如果你使用find
函数,那就是这样:
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
让我知道它是否有效。
答案 2 :(得分:0)
我会删除循环中单元格的所有读写:
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