[英]Look up values in sheet(x) column(x), match to values in sheet(y) column(y), if they match paste row
Dealing with an issue that seems simple enough, but for some reason I cannot get this to work. 处理似乎很简单的问题,但是由于某种原因,我无法解决这个问题。
I have a data input sheet I am trying to match values across to another sheet, the values are both in column E, and all the values in column E are unique. 我有一个数据输入表,我正在尝试将值与另一个表进行匹配,这些值都在E列中,并且E列中的所有值都是唯一的。 The values will always be stored in rows 8 though to 2500.
值将始终存储在第8行至2500中。
My code is as below, however is throwing the ever useful 1004 error (Application-Defined or object-defined error), on line 我的代码如下,但是在网上抛出了永远有用的1004错误(应用程序定义的错误或对象定义的错误)
If Sheets("Target Inputs").Range("E" & CStr(LSearchRow)).Value = searchTerm Then
any help would be greatly appreciated: 任何帮助将不胜感激:
Sub LOAD_BUID_Lookup()
Dim i As Integer
Dim LSearchRow As Integer
Dim LCopyToRow As Integer
Dim searchTerm As String
On Error GoTo Err_Execute
For i = 8 To 2500
searchTerm = Range("E" & i).Text
If Sheets("Target Inputs").Range("E" & CStr(LSearchRow)).Value = searchTerm Then
'Select row in Sheet1 to copy
Rows(CStr(LSearchRow) & ":" & CStr(LSearchRow)).Select
Selection.Copy
'Paste row into Sheet2 in next row
Sheets("LOAD").Select
Rows(CStr(LCopyToRow) & ":" & CStr(LCopyToRow)).Select
ActiveSheet.Paste
'Move counter to next row
LCopyToRow = LCopyToRow + 1
'Go back to Sheet1 to continue searching
Sheets("Target Inputs").Select
End If
Next i
Application.CutCopyMode = False
Range("A3").Select
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
没有将LSearchRow
设置为任何值,这意味着它为0。这又引发了异常,因为行号不能为0。而且没有任何理由使用CStr
转换为字符串,因为串联会转换整个范围参数仍然是字符串。
Usually when comparing two different columns in two different sheet you would see a double loop the first to loop through sheet1 and the second to take every value of sheet1 and loop through sheet2 to find a match. 通常,当比较两个不同工作表中的两个不同列时,您会看到一个双循环,第一个循环通过sheet1,第二个循环获取sheet1的每个值,然后循环通过sheet2查找匹配项。 In reading your description I think this is what you want.
在阅读您的描述时,我认为这就是您想要的。
Double loops can be time intensive. 双循环可能会占用大量时间。 There is another way,
Worksheetfunction.match
!! 还有另一种方法,
Worksheetfunction.match
!
I also noticed your code selecting/activating sheets multiple times. 我还注意到您的代码多次选择/激活工作表。 Typically selecting/activating sheets is not required if you declare and instantiate the variables you need.
通常,如果声明并实例化所需的变量,则不需要选择/激活工作表。
Below is an example code I tried to make it as plug and play as possible, but I wasn't sure of the name of the sheet you are looping through. 下面是一个示例代码,我试图使其尽可能地即插即用,但是我不确定要遍历的工作表的名称。 I've tested the code on dummy data and it seems to work, but again I'm not quite positive on the application.
我已经在伪数据上测试了代码,它似乎可以正常工作,但是我对应用程序也不是很满意。 I've commented the code to explain as much of the process as possible.
我已经注释了代码,以解释尽可能多的过程。 Hopefully it helps.
希望它会有所帮助。 Cheers!
干杯!
Option Explicit 'keeps simple errors from happening
Sub LOAD_BUID_Lookup()
'Declare variables
Dim wb As Workbook
Dim wsInputs As Worksheet
Dim wsTarget As Worksheet
Dim wsLoad As Worksheet
Dim searchTerm As String
Dim matchRng As Range
Dim res
Dim i As Integer
'instantiate variables
Set wb = Application.ThisWorkbook
Set wsInputs = wb.Worksheets("Inputs") 'unsure of the name of this sheet
Set wsTarget = wb.Worksheets("Target Inputs")
Set wsLoad = wb.Worksheets("LOAD")
Set matchRng = wsTarget.Range("E:E")
On Error GoTo Err_Execute
For i = 8 To 2500
searchTerm = wsInputs.Range("E" & i).Text 'can use sheet variable to refer exactly to the sheet you want without selecting
'get match if one exists
On Error Resume Next
res = Application.WorksheetFunction.Match(searchTerm, matchRng, 0) 'will return a row number if there is a match
If Err.Number > 0 Then 'the above command will throw an error if there is no match
'MsgBox "No Match!", vbCritical
Err.Clear ' we clear the error for next time around
On Error GoTo 0 'return to previous error handeling
Else
On Error GoTo 0 'return to previous error handeling
wsInputs.Range("A" & i).EntireRow.Copy Destination:=wsLoad.Range("A" & wsLoad.Range("E50000").End(xlUp).Row + 1) 'gets last row and comes up to last used row ... offset goes one down from that to the next empty row
End If
Next i
'Application.CutCopyMode = False -- there is no need for this when we use "Destination"
MsgBox "All matching data has been copied."
Exit Sub
Err_Execute:
MsgBox "An error occurred."
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.