我正在嘗試通過vba中的查找功能來查找vlookup。我在貸款表和屬性表中有一個數字列表,如果在貸款表中找到了這個數字,它會複製整行並將其粘貼到另一個名爲query的表中。這是我目前的代碼,但代碼只是掛起,因爲我有太多的單元格來找到大約100,000個。代碼中任何錯誤的任何指導都會很有幫助。在一個循環VBA:查找功能代碼
Option Explicit
Sub FindCopy_lall()
Dim calc As Long
Dim Cel As Range
Dim LastRow As Long
Dim LastRow2 As Long
Dim rFound As Range
Dim LookRange As Range
Dim CelValue As Variant
' Speed
calc = Application.Calculation
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'Get Last row of Property SheetColumn
LastRow = Worksheets("Property").Cells(Rows.Count, "E").End(xlUp).Row
LastRow2 = Worksheets("Loan").Cells(Rows.Count, "D").End(xlUp).Row
' Set range to look in
Set LookRange = Worksheets("Property").Range("E2:E" & LastRow)
' Loop on each value (cell)
For Each Cel In LookRange
' Get value to find
CelValue = Cel.Value
' Look on IT_Asset
' With Worksheets("Loan")
' Allow not found error
On Error Resume Next
Set rFound = Worksheets("Loan").Range("D2:D" & LastRow2).Find(What:=CelValue, _
LookIn:=xlValues, _
Lookat:=xlWhole, MatchCase:=False)
' Reset
On Error GoTo endo
' Not found, go next
If rFound Is Nothing Then
GoTo nextCel
Else
Worksheets("Loan").Range("rFound:rFound").Select
Selection.Copy
Worksheets("Query").Range("Cel:Cel").Select
ActiveSheet.Paste
End If
'End With
nextCel:
Next Cel
'Reset
endo:
With Application
.Calculation = calc
.ScreenUpdating = True
End With
End Sub
您是否確實需要複製/粘貼找到的行中的所有內容?或者粘貼值是否足夠? – RBarryYoung 2013-04-26 15:50:30
粘貼值就足夠了 – codemacha 2013-04-26 15:53:57
@RBarryYoung:我猜粘貼值會幫助提高速度,但我不確定代碼是否正確執行 – codemacha 2013-04-26 16:00:43