简体   繁体   English

Excel宏-如何根据特定的单元格值复制/拆分行

[英]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: 为了扩展行,该附加宏将遵循以下模式:

  • Loop thru your data, starting at the last row 从最后一行开始遍历数据
  • If Quantity > 1, 如果数量> 1,
  • Insert rows to make space 插入行以腾出空间
  • copy row data down 向下复制行数据
  • set Quantity to 1 将数量设置为1

.

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.

相关问题 Excel宏,要基于另一个单元格值复制和粘贴单元格值? - Excel macro, to copy and paste a cell value based on another cell value? VBA Excel:宏,该宏在一行中搜索特定单元格中的某个变量值,然后将值复制并粘贴到此列中 - VBA Excel : Macro that searches a row for a certain variable value from specific cell and then copy&pastes value to this column 如何根据单元格中的值将 Excel 行拆分为多个 - How to split Excel row into multiple based on value in cell Excel宏可根据单元格值将一行中的选定单元格从一张纸复制到另一张纸 - Excel Macro to copy select cells from a row from one sheet to another based upon a cell value 宏根据特定的单元格值将行粘贴范围复制到不同的工作表中 - Macro to copy-paste range in row to different sheets based on specific cell value 根据特定的单元格值复制一行openpyxl - Copy a row based on a specific cell value openpyxl 基于单元格值的Excel宏复制范围粘贴偏移量 - Excel Macro Copy Range Paste offset based on cell value Excel宏,根据另一个单元格值复制和粘贴多个单元格? - Excel macro, to copy and paste a multiple cells based on another cell value? VBA宏-根据单元格值从下面复制部分行 - VBA Macro - Copy Partial Row from Below Based on Cell Value Excel宏可根据列和单元格值更改行的颜色 - Excel Macro to Change Color of a Row Based on Column and Cell Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM