[英]vba excel vlookup using arrays
I Have the following code, which works, but is a bit slow, I would like to do the same but using arrays 我有下面的代码,可以工作,但是有点慢,我想做同样的事情,但使用数组
Sub AddValues()
Dim Srng As Range
Dim search_value As Variant
PG = "Data"
Ln = 2
Set Srng = Worksheets("Coniguration").Range("_Configuration")
LastRow = Worksheets("Data").Range("A1").CurrentRegion.Rows.Count
For Ln = 2 To LastRow
search_value = Val(ActiveWorkbook.Sheets(PG).Cells(Ln, "A").Value)
ActiveWorkbook.Sheets("Data").Cells(Ln, "CA").Value = Application.VLookup(search_value, Srng, 3, False)
ActiveWorkbook.Sheets("Data").Cells(Ln, "CB").Value = Application.VLookup(search_value, Srng, 4, False)
ActiveWorkbook.Sheets("Data").Cells(Ln, "CC").Value = Application.VLookup(search_value, Srng, 5, False)
ActiveWorkbook.Sheets("Data").Cells(Ln, "CD").Value = Application.VLookup(search_value, Srng, 6, False)
ActiveWorkbook.Sheets("Data").Cells(Ln, "CF").Value = Application.VLookup(search_value, Srng, 7, False)
Next Ln
End Sub
One sure source of slowness is that you are doing the same search 5 times in each iteration. 缓慢的一个可靠来源是,您在每次迭代中进行了5次相同的搜索。 You can instead find the matching row only once, then copy the cells from the matched row.
相反,您只能找到匹配的行一次,然后从匹配的行中复制单元格。 Also interesting is to get a sheet reference once and avoid fetching the worksheet with
Worksheets(name)
in every iteration. 同样有趣的是,一次获取工作表引用,并避免在每次迭代中都使用
Worksheets(name)
获取工作Worksheets(name)
。
Sub AddValues()
Dim Srng As Range, Ln As Long, matchRow, search_value
Set Srng = Worksheets("Configuration").Range("_Configuration")
With Worksheets("Data")
For Ln = 2 To .Cells(.Rows.count, "A").End(xlUp).row
search_value = val(.Cells(Ln, "A").Value2)
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
' Find matching row only once and copy the results
matchRow = Application.match(search_value, Srng.Columns(1), 0)
If IsError(matchRow) Then
Debug.Print search_value & " : Not found"
Else
.Cells(Ln, "CA").Resize(, 4).value = Srng.Cells(matchRow, 3).Resize(, 4).Value2
.Cells(Ln, "CF").value = Srng.Cells(matchRow, 7).Value2
End If
Next Ln
End With
End Sub
Here's a method that avoids looping. 这是一种避免循环的方法。 First it enters the formula in the target cells, and then it converts the formulas into values.
首先,它在目标单元格中输入公式,然后将公式转换为值。
Sub AddValues()
Dim Srng As Range
Dim LastRow As Long
Set Srng = Worksheets("Coniguration").Range("_Configuration")
With Worksheets("Data")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Range("CA2:CA" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC1," & Srng.Address(, , xlR1C1, True) & ", 3, 0)"
.Value = .Value
End With
With .Range("CB2:CB" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC1," & Srng.Address(, , xlR1C1, True) & ", 4, 0)"
.Value = .Value
End With
With .Range("CC2:CC" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC1," & Srng.Address(, , xlR1C1, True) & ", 5, 0)"
.Value = .Value
End With
With .Range("CD2:CD" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC1," & Srng.Address(, , xlR1C1, True) & ", 6, 0)"
.Value = .Value
End With
With .Range("CF2:CF" & LastRow)
.FormulaR1C1 = "=VLOOKUP(RC1," & Srng.Address(, , xlR1C1, True) & ", 7, 0)"
.Value = .Value
End With
End With
End Sub
Thank you very much ASH and Domenic, both methods work much better than my code. 非常感谢ASH和Domenic,这两种方法都比我的代码好得多。
At the end I'll use the one provided by Domenic as it is the fastest one. 最后,我将使用Domenic提供的一种,因为它是最快的一种。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.