![](/img/trans.png)
[英]Can I use vlookup or match/index to look up data to the left for more than look up value?
[英]Faster way to look up multiple columns of data than VLookup in VBA?
我有一個大型數據處理電子表格,該電子表格將查找組件編號,然后將相關列加載到數組中。 目前,我正在使用VLookup函數,這是一個非常慢的函數,它在循環中使用數千次。
我當前的代碼部分:
Set drng = Sheets(Data).Range("D2:AS" & imax)
On Error Resume Next
For i = 1 To 7
jmax = Sheets(ShtName(i)).UsedRange.Rows.Count
For j = 3 To jmax
Component= Sheets(ShtName(i)).Cells(j, 1).Value2
DataVar(1) = Application.VLookup(Component, drng, 32, False)
DataVar(2) = Application.VLookup(Component, drng, 35, False)
DataVar(3) = Application.VLookup(Component, drng, 42, False)
DataVar(4) = Application.VLookup(Component, drng, 11, False)
DataVar(5) = Application.VLookup(Component, drng, 15, False)
DataVar(6) = Application.VLookup(Component, drng, 24, False)
DataVar(7) = Application.VLookup(Component, drng, 18, False)
DataVar(8) = Application.VLookup(Component, drng, 38, False)
DataVar(9) = Application.VLookup(Component, drng, 21, False)
DataVar(10) = Application.VLookup(Component, drng, 29, False)
Next j
Next i
On Error GoTo 0
有沒有更快的方法?
由於您希望同一行中有多列信息,並且知道哪些列具有該信息,因此您要做的就是找到數據所在的行,然后直接引用單元格。 您可以使用“匹配”功能確定行。
參見以下代碼:
For i = 1 To 7
jmax = Sheets(ShtName(i)).UsedRange.Rows.Count
For j = 3 To jmax
Component = Sheets(ShtName(i)).Cells(j, 1).Value2
With Sheets(Quarter)
ComponentRow = WorksheetFunction.Match(Component, .Range("D:D"), 0)
DataVar(1) = .Cells(ComponentRow, 35).Value2
DataVar(2) = .Cells(ComponentRow, 38).Value2
DataVar(3) = .Cells(ComponentRow, 45).Value2
DataVar(4) = .Cells(ComponentRow, 14).Value2
DataVar(5) = .Cells(ComponentRow, 18).Value2
DataVar(6) = .Cells(ComponentRow, 27).Value2
DataVar(7) = .Cells(ComponentRow, 21).Value2
DataVar(8) = .Cells(ComponentRow, 41).Value2
DataVar(9) = .Cells(ComponentRow, 24).Value2
DataVar(10) = .Cells(ComponentRow, 32).Value2
End With
Next j
Next i
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.