【问题标题】:Insert Multiple Rows under Active Cell using VBA in Excel在 Excel 中使用 VBA 在活动单元格下插入多行
【发布时间】:2019-02-03 08:21:22
【问题描述】:

我正在编写一个贯穿每一行的宏(某些行包含的列比其他行多)并计算列数并将相同的数字插入所选行下方的空白单元格。目标是最终将数据复制并粘贴到单个列中。

我能够插入正确数量的空白单元格,但它们被插入到所选行的上方。我希望将它们插入所选行的下方。对此的任何帮助将不胜感激!

    rowCount = ActiveSheet.Cells(rows.Count, 5).End(xlUp).Row
    ActiveSheet.Range("E1:E" & rowCount).Select
    For c = 1 To rowCount
        columnCount = ActiveSheet.Cells(c, 
        Columns.Count).End(xlToLeft).Column - 5
            If columnCount > 0 Then
                Let CopyRange = "E" & c & ":" & "E" & (c + columnCount - 1)
                ActiveSheet.Range(CopyRange).EntireRow.Insert Shift:=xlDown
            End If
        rowCount = ActiveSheet.Cells(rows.Count, 5).End(xlUp).Row
        c = c + columnCount
    Next c

【问题讨论】:

    标签: vba excel insert rows


    【解决方案1】:

    在您当前插入的行下方插入 1 行。此外,您可以通过向后循环来简化此操作。

    rowCount = ActiveSheet.Cells(rows.Count, 5).End(xlUp).Row
    For c = rowCount to 1 step -1
        columnCount = ActiveSheet.Cells(c, Columns.Count).End(xlToLeft).Column - 5
            If columnCount > 0 Then
                Set CopyRange = "E" & c + 1 & ":" & "E" & (c + columnCount)
                ActiveSheet.Range(CopyRange).EntireRow.Insert Shift:=xlDown
            End If
    Next c
    

    注意:这是未经测试的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-26
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      相关资源
      最近更新 更多