gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:28:58 25 4
gpt4 key购买 nike

我正在尝试以一种可以节省我无数小时的繁琐数据输入的方式实现 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”次(到单独的工作表中)

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

最佳答案

大卫比我先一步,但替代方法不会伤害任何人。

考虑以下数据

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 代替您的情况。

关于Excel VBA 自动化 - 根据单元格值复制行 "x"次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25395454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com