从两张纸复制,插入行和粘贴数据到第三张纸

时间:2020-03-30 05:11:39

标签: excel vba insert copy paste

我有两张纸的数据,一张纸有客户,另一张纸有材料数据。在两个工作表中,“基本客户”中的唯一列。在客户表中,每个基本客户有1个或多个不同的客户,物料单也一样,就像每个基本客户具有不同的材料一样。我想将两个工作表数据合并到新工作表中。

例如-

Sheet1具有以下数据

Base cust   customer

00301        112501
00301        112502
00302        112503
00302        112504

第2页包含以下数据

Base Cust    Material

00301        M12113501
00301        M12113502
00302        M12113501
00302        M12113502

在新工作表中,数据应类似于

Base Cust     Cust          Material

00301        112501         M12113501
00301        112501         M12113502
00301        112502         M12113501
00301        112502         M12113502
00302        112503         M12113501
00302        112503         M12113502
00302        112504         M12113501
00302        112504         M12113502

如果您可以为我提供VBA代码以准备上述此类数据,则需要您的帮助。

Sub Macro() 

    Dim lastrowone, lastrowtwo As Long 
    lastrowone = Sheets("1").Cells(Rows.Count, 1).End(xlUp).Row 
    lastrowtwo = Sheets("2").Cells(Rows.Count, 1).End(xlUp).Row 
    For I = 2 To lastrowone 
        For J = 2 To lastrowtwo 
            If Sheets("1").Cells(I, 1).Value = Sheets("2").Cells(J, 1).Value Then 
                Sheets("1").Cells(I, 1).EntireRow.Copy 
                Sheets("3").Cells(I, 1).Offset(1).Insert Shift:=xlDown 
             End If 
        Next J 
    Next I 

End Sub

0 个答案:

没有答案
相关问题