简体   繁体   English

Excel Vba-如何将匹配的行从一个工作表复制并粘贴到另一工作表中的完全匹配的行以下

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

相关问题 如何使用 VBA 宏从一张工作表中复制“特定”行并粘贴到另一个工作表中 - How to copy "specific" rows from one sheet and paste in to another in an excel using VBA Macros VBA代码将行从一个Excel工作表复制到另一个 - VBA code to copy rows from one excel sheet to another Excel VBA 复制多个范围并粘贴到另一个工作表,没有空行 - Excel VBA to Copy Multiple Ranges and paste to another sheet, no empty rows 将太多时间从一个Excel工作表行(匹配的行)复制到另一个Excel工作表-VBA - Taking too much time to copy from one excel sheet row (matching rows) to another excel sheet - VBA 在Excel中,如何将单元格与另一张工作表中的列进行比较,以及如何将匹配的实例用于来自相应行的值的总和 - In Excel, how to compare a cell to a column in another sheet and use the matched instances for sum of a value from the corresponding rows 将行值从一张工作表复制到另一张工作表中以匹配Excel中的记录 - copy row values from one sheet to another sheet for the matched records in Excel Excel - 根据没有 VBA 的条件将行从一个工作表复制到另一个工作表 - Excel - Copy rows from one sheet to another sheet based in a criteria without VBA 如何将行从一个Excel工作表复制到另一个工作表,并使用VBA创建重复项? - How to copy rows from one excel sheet to another and create duplicates using VBA? 使用excel VBA将数据从一张纸复制并粘贴到另一张纸,然后从第二张纸复制到第三张纸 - Copy and paste data from one a sheet to another sheet, and from second sheet to third using excel VBA 如何将行从一个Excel工作表剪切/粘贴到另一工作表的第一空白行 - How to cut/paste rows from one Excel sheet to 1st blank row of another sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM