[英]Excel Macro - How to copy/split row based on specific cell value
I'm dealing with a huge spreadsheet and needed some help with a particular Macro I want to write for it. 我正在处理一个巨大的电子表格,并且需要一些有关我要为其编写的特定宏的帮助。
I have a whole bunch of information pertaining to what equipment goes in which room. 我有一堆信息,涉及哪些设备进入哪个房间。 Each room has its own row for the type of equipment being installed. 每个房间针对要安装的设备类型都有自己的一行。 Sometimes one room has more than one of the same equipment and is specified in the quantity column. 有时,一个房间有多个相同的设备,并在数量列中指定。 I need to split/copy such rows so that each equipment has its own row. 我需要拆分/复制这样的行,以便每个设备都有自己的行。
What I have currently: 我目前所拥有的:
A B C
Equip. Name Rm Number Quantity
xxxxx 1.2.3.4 5
yyyyy 1.2.3.4 1
What I need the macro to do for me: Find and copy all the rows with quantity greater than 1 into the following rows below the same number times as the quantity value and replace it all with quantity of 1 for the whole spreadsheet. 我需要宏为我执行的操作:查找数量大于1的所有行并将其复制到下面的行中,数量与数量值相同,且数量相同,并在整个电子表格中将其全部替换为数量1。
A B C
Equip. Name Rm Number Quantity
xxxxx 1.2.3.4 1
xxxxx 1.2.3.4 1
xxxxx 1.2.3.4 1
xxxxx 1.2.3.4 1
xxxxx 1.2.3.4 1
yyyyy 1.2.3.4 1
Thank you in advance. 先感谢您。
To expand the rows in place, thae attached macro will follow this pattern: 为了扩展行,该附加宏将遵循以下模式:
. 。
Sub ExpandRows()
Dim dat As Variant
Dim i As Long
Dim rw As Range
Dim rng As Range
Set rng = ActiveSheet.UsedRange
dat = rng
' Loop thru your data, starting at the last row
For i = UBound(dat, 1) To 2 Step -1
' If Quantity > 1
If dat(i, 3) > 1 Then
' Insert rows to make space
Set rw = rng.Rows(i).EntireRow
rw.Offset(1, 0).Resize(dat(i, 3) - 1).Insert
' copy row data down
rw.Copy rw.Offset(1, 0).Resize(dat(i, 3) - 1)
' set Quantity to 1
rw.Cells(1, 3).Resize(dat(i, 3), 1) = 1
End If
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.