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

Jud*_*nna 4 excel automation vba rows duplicates

我试图以一种能够节省数不清的繁琐数据输入的方式自动化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"次(到单独的表)

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

Mat*_*att 7

大卫打败了我,但另一种方法从来没有伤害任何人.

请考虑以下数据

Item           Cost Code         Quantity
Fiddlesticks   0.8  22251554787  0
Woozles        1.96 54645641     3
Jarbles        200  158484       4
Yerzegerztits  56.7 494681818    1
Run Code Online (Sandbox Code Playgroud)

有了这个功能

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
Run Code Online (Sandbox Code Playgroud)

在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
Run Code Online (Sandbox Code Playgroud)

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