简体   繁体   English

在sheet(x)列(x)中查找值,如果它们与粘贴行匹配,则与sheet(y)列(y)中的值匹配

[英]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.

相关问题 在列中查找值并匹配 - Look for values in column and match 如果在Y列中匹配,则向X列中的单元格添加文本 - Add text to a cell in column X if match in column Y 如何出于复制粘贴的目的,选择一列(基于另一列)中的某些值与另一张工作表中的列相匹配? - How do I choose certain values in one column (based on another) to match with a column in another sheet, for the purpose of copy-paste? 如果工作表“ X”中的单元格包含工作表“ y”的名称,请删除行工作表“ x” - Delete row sheet 'x' if cell in sheet 'X' contains name of sheet 'y' 检查列中的值是否与其他工作表中的范围匹配 - Check if the values from a column match the range from a different sheet VBA - 如果行不存在,则根据 A 列和 C 列将行从工作表 X 复制到工作表 Y - VBA - Copy rows from a sheet X to a sheet Y if rows doesn't already exists, based on column A and column C 使用VBA中的应用程序工作表功能从一个工作表中复制列并粘贴为另一行中的行 - Copy a column from one sheet and paste as row in another using application worksheetfunciton match in vba 将 Sheet1 列 A 的值与 Sheet2 列 B 的值进行比较,如果匹配,则 Sheet2.Col C=Sheet1.Col A 和 Sheet2.Col D=True - Compare the Sheet1 column A values with Sheet2 column B, if match then Sheet2.Col C=Sheet1.Col A and Sheet2.Col D=True 当列 X 的值与列 Y 的值不匹配时,VBA 如何删除行 - VBA How to Delete Rows when value of Column X does not match value of column Y Sheet.Range(Sheet,Cells(x,y), Sheet.Cells(x2,y2)) 语法 - Sheet.Range(Sheet,Cells(x,y), Sheet.Cells(x2,y2)) Syntax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM