基于一系列数据动态复制和复制

时间:2015-09-03 20:58:47

标签: excel copy row

我发现如何复制但不能复制和复制基于一系列数据,当数据更改时复制第一行并插入数据

我有一组没有父sku的子sku,我试图找出在子SKU上面插入父SKU的最佳方法。

这是我的数据样本,插入的行按照我需要的方式插入:

36377-CSG/PDB-46    36377CSG/PDB46  ABC Brand   ABC Product     [S]Size=34,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-CSG/PDB-46    36377CSG/PDB46  ABC Brand   ABC Product     [S]Size=34,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-AG/BL-46      36377AG/BL46    ABC Brand   ABC Product     [S]Size=34,[S]Color=Ash Grey/Black  36377
36377-AG/BL-36      36377AG/BL36    ABC Brand   ABC Product     [S]Size=24,[S]Color=Ash Grey/Black  36377
36377-AG/BL-44      36377AG/BL44    ABC Brand   ABC Product     [S]Size=32,[S]Color=Ash Grey/Black  36377
36377-CSG/PDB-44    36377CSG/PDB44  ABC Brand   ABC Product     [S]Size=32,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-CSG/PDB-34    36377CSG/PDB34  ABC Brand   ABC Product     [S]Size=22,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-AG/BL-38      36377AG/BL38    ABC Brand   ABC Product     [S]Size=26,[S]Color=Ash Grey/Black  36377
36377-AG/BL-42      36377AG/BL42    ABC Brand   ABC Product     [S]Size=30,[S]Color=Ash Grey/Black  36377
36377-CSG/PDB-40    36377CSG/PDB40  ABC Brand   ABC Product     [S]Size=28,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-CSG/PDB-42    36377CSG/PDB42  ABC Brand   ABC Product     [S]Size=30,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-AG/BL-40      36377AG/BL40    ABC Brand   ABC Product     [S]Size=28,[S]Color=Ash Grey/Black  36377
36377-CSG/PDB-36    36377CSG/PDB36  ABC Brand   ABC Product     [S]Size=24,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
36377-CSG/PDB-38    36377CSG/PDB38  ABC Brand   ABC Product     [S]Size=26,[S]Color=Cobblestone Grey/ Peacoat Dark Blue 36377
24503-PFPI-85       24503PFPI85     ABC Brand   DEF Product     [S]Size=51,[S]Color=Parfait Pink    24503
24503-PFPI-85       24503PFPI85     ABC Brand   DEF Product     [S]Size=51,[S]Color=Parfait Pink    24503
24503-SPG-95        24503SPG95      ABC Brand   DEF Product     [S]Size=57,[S]Color=Steel Grey  24503
24503-PFPI-95       24503PFPI95     ABC Brand   DEF Product     [S]Size=57,[S]Color=Parfait Pink    24503
24503-PFPI-105      24503PFPI105    ABC Brand   DEF Product     [S]Size=60,[S]Color=Parfait Pink    24503
24503-SPG-115       24503SPG115     ABC Brand   DEF Product     [S]Size=66,[S]Color=Steel Grey  24503
24503-SPG-75        24503SPG75      ABC Brand   DEF Product     [S]Size=45,[S]Color=Steel Grey  24503
24503-SPG-125       24503SPG125     ABC Brand   DEF Product     [S]Size=69,[S]Color=Steel Grey  24503
24503-SPG-85        24503SPG85      ABC Brand   DEF Product     [S]Size=51,[S]Color=Steel Grey  24503

Sample data

我需要的是每次g列中的SKU更改时,该组的整个顶行都要复制并插入到该集合之上。只要它是任何行的副本,在哪个行被复制和插入上面并不重要。因此,例如在G 24503列中,它将复制第16行,插入新行并复制第16行上方第16行的内容。它将对G列中每个新的SKU集执行此操作。我不需要每行复制只是每组一个新线。希望有道理。如果我能得到它,我有办法粘贴父sku已经我只需要复制和插入每个部分中的那一行(虽然不是一个空行,它需要是整行。

1 个答案:

答案 0 :(得分:0)

完全自动插入最好由Worksheet_Change事件宏处理。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns(7)) Is Nothing Then  '<~~something happened in column G
        On Error GoTo bm_Safe_Exit
        Application.EnableEvents = False
        Dim fPos As Long, pSku As Range
        For Each pSku In Intersect(Target, Columns(7))
            fPos = Application.Match(pSku.Value2, Columns(7), 0)
            If Application.CountIf(Columns(1), Cells(fPos, 1).Value2) = 1 Then
                With Cells(fPos, 1).Resize(1, 7)
                    .Copy
                    .Insert Shift:=xlDown
                End With
            End If
        Next pSku
        Application.CutCopyMode = False
    End If
bm_Safe_Exit:
    Application.EnableEvents = True
End Sub

尽管我尽最大努力格式化您的样本数据,但右侧的SKU仍然作为F列进入,而不是G.上面的代码是在对列(7)的更改时触发的;又名列G.它设计用于同时存活多个SKU插入;从复制和粘贴或许。