[英]Excel Vba - How to copy and paste matched rows from one sheet to below exact matched rows in another sheet
I am fairly new in excel vba scenario. 我在excel vba场景中还很新。 What I am trying to accomplish here in this macro is, 我要在此宏中完成的任务是,
I have two sheet, two column, sheet1 Column A, sheet2 Column A, both have possible matches in column A. I am trying to find all the matches between two sheets and copy matched entire rows from sheet1 to exactly below matched rows in sheet two with the header of sheet1. 我有两张纸,两列,sheet1列A,sheet2列A,在A列中都有可能的匹配项。我试图找到两张纸之间的所有匹配项,并将匹配的整个行从sheet1复制到恰好在第二张中的匹配行以下与sheet1的标题。
sheet1 工作表Sheet1
Data-----------name 数据-----------名
012-----------AAA 012 ----------- AAA
022-----------BBB 022 ----------- BBB
033-----------CCC 033 ----------- CCC
Sheet2 Sheet2中
id-----------address ID -----------地址
012-----------NYC 012 ----------- NYC
021-----------Philly 021 -----------费城
033-----------CT 033 ----------- CT
Result 结果
id-----------address ID -----------地址
012-----------NYC 012 ----------- NYC
Data-----------name 数据-----------名
012-----------AAA 012 ----------- AAA
021-----------Philly 021 -----------费城
033-----------CT 033 ----------- CT
Data-----------name 数据-----------名
033-----------CCC 033 ----------- CCC
The code I have so far only copying the first row, no idea how to fix it. 到目前为止,我所拥有的代码仅复制第一行,不知道如何解决。
Sub oneMacro()
Dim lastrowone As Integer, lastrowtwo As Integer
lastrowone = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lastrowtwo = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrowone
For j = 2 To lastrowtwo
If Sheets("Sheet1").Cells(i, "A").Value = Sheets("Sheet2").Cells(j, "A").Value Then
Sheets("Sheet1").Cells(i, "A").EntireRow.Copy
Sheets("Sheet2").Cells(j, "A").Offset(1).Insert Shift:=xlDown
End If
Next j
Next i
End Sub
There are a couple of problems with your code. 您的代码有两个问题。 First off, to help you learn how you could trouble shoot this... First you would want to add some breakpoints, and setup a few watches. 首先,为了帮助您了解如何解决此问题...首先,您需要添加一些断点,并设置一些手表。 But you will see that your loops are setup perfecly at first, but do not adapt properly as you add data. 但是您会看到循环一开始就已完全设置,但是在添加数据时无法正确适应。
Pretty much your loop statement continues looping until the your hit lastrowtwo
which at first is set for a value of 3 (based on your example above). 几乎您的loop语句继续循环,直到您命中的lastrowtwo
最初设置为3(基于上面的示例)。 Instead your code needs to add +1
each time you find a true result to the lastrowtwo
variable. 相反,每次您向lastrowtwo
变量找到真实结果时,您的代码就需要加+1
。 I have modified your code below to overcome this issue. 我已经在下面修改了您的代码以解决此问题。
Another issue is that you are coping everything from one cell to another, then shifting it down. 另一个问题是您正在处理从一个单元到另一个单元的所有内容,然后将其向下移动。 When doing this, you will comparing that next (which will come back as being a match). 这样做时,您将比较下一个(作为匹配项返回)。 After a while you will see that this only will scan the first line item. 片刻之后,您会看到这只会扫描第一个订单项。 To overcome this you can simply skip the next line in the loop check statement. 为了克服这个问题,您可以简单地跳过循环检查语句中的下一行。 You can do this by adding +1
to the j
variable. 您可以通过将j
变量加+1
来实现。 See below for the modifications. 参见下面的修改。
Sub oneMacro()
Dim lastrowone, lastrowtwo As Long
lastrowone = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lastrowtwo = Sheets("Sheet2").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lastrowone
For j = 2 To lastrowtwo
If Sheets("Sheet1").Cells(i, 1).Value = Sheets("Sheet2").Cells(j, 1).Value Then
Sheets("Sheet1").Cells(i, 1).EntireRow.Copy
Sheets("Sheet2").Cells(j, 1).Offset(1).Insert Shift:=xlDown
j = j + 1 ' Modified = this must be added to overcome an issue with DOUBLE checking the newly inserted data
lastrowtwo = lastrowtwo + 1 ' Modified = This is added to overcome an issue with not completing all rows
End If
Next j
Next i
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.