Excel VBA自动化 - 根据单元格值复制行“x”次

时间:2014-08-20 01:32:11

标签: excel vba automation duplicates rows

我试图以一种能够为我节省无数繁琐数据的方式来自动化Excel。这是我的问题。

我们需要为所有库存打印条形码,其中包括4,000种变体,每种变体都有特定的数量。

Shopify是我们的电子商务平台,他们不支持定制出口;但是,可以导出所有变体的CSV,其中包括库存盘点列。

我们使用Dymo作为条形码打印硬件/软件。 Dymo每行只打印一个标签(忽略数量列)。

有没有办法让excel自动复制行" x"基于广告资源列中的值的次数?

以下是数据样本:

https://www.evernote.com/shard/s187/sh/b0d5b92a-c5f6-469c-92fb-3d4e03d97544/d176d3448ba0cafbf3d61506402d9e8b/res/254447d2-486d-454f-8871-a0962f03253d/skitch.png

  • 如果列N = 0,则忽略并移至下一行
  • 如果列N> 1,复制当前行," N"次数(单独的表格)

我试图找到一个做过类似事情的人,以便我可以修改代码,但经过一个小时的搜索,我仍然在我开始的地方。提前感谢您的帮助!

3 个答案:

答案 0 :(得分:7)

大卫打败了我,但另一种方法从不伤害任何人。

考虑以下数据

Item           Cost Code         Quantity
Fiddlesticks   0.8  22251554787  0
Woozles        1.96 54645641     3
Jarbles        200  158484       4
Yerzegerztits  56.7 494681818    1

使用此功能

Public Sub CopyData()
    ' This routing will copy rows based on the quantity to a new sheet.
    Dim rngSinglecell As Range
    Dim rngQuantityCells As Range
    Dim intCount As Integer

    ' Set this for the range where the Quantity column exists. This works only if there are no empty cells
    Set rngQuantityCells = Range("D1", Range("D1").End(xlDown))

    For Each rngSinglecell In rngQuantityCells
        ' Check if this cell actually contains a number
        If IsNumeric(rngSinglecell.Value) Then
            ' Check if the number is greater than 0
            If rngSinglecell.Value > 0 Then
                ' Copy this row as many times as .value
                For intCount = 1 To rngSinglecell.Value
                    ' Copy the row into the next emtpy row in sheet2
                    Range(rngSinglecell.Address).EntireRow.Copy Destination:= Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1)                                
                    ' The above line finds the next empty row.

                Next
            End If
        End If
    Next
End Sub

在sheet2上生成以下输出

Item            Cost    Code        Quantity
Woozles         1.96    54645641    3
Woozles         1.96    54645641    3
Woozles         1.96    54645641    3
Jarbles         200     158484      4
Jarbles         200     158484      4
Jarbles         200     158484      4
Jarbles         200     158484      4
Yerzegerztits   56.7    494681818   1

使用此代码的警告是“数量”列中不能有空字段。我用过D,所以请随意用N代替你的情况。

答案 1 :(得分:2)

应该足以让你入门:

Sub CopyRowsFromColumnN()

Dim rng As Range
Dim r As Range
Dim numberOfCopies As Integer
Dim n As Integer

'## Define a range to represent ALL the data
Set rng = Range("A1", Range("N1").End(xlDown))

'## Iterate each row in that data range
For Each r In rng.Rows
    '## Get the number of copies specified in column 14 ("N")
    numberOfCopies = r.Cells(1, 14).Value

    '## If that number > 1 then make copies on a new sheet
    If numberOfCopies > 1 Then
        '## Add a new sheet
        With Sheets.Add
            '## copy the row and paste repeatedly in this loop
            For n = 1 To numberOfCopies
                r.Copy .Range("A" & n)
            Next
        End With
    End If
Next

End Sub

答案 2 :(得分:1)

可能有点迟,但这可以帮助别人。 我在Excel 2010上测试了这个解决方案。 说:“Sheet1”是数据所在工作表的名称  和“Sheet2”是您想要重复数据的工作表。 假设您已创建这些工作表,请尝试以下代码。

16:00

此解决方案是David Zemens解决方案的略微修改版本。

相关问题