【问题标题】:Create Rows, then Copy and Paste Original Row into New Rows创建行,然后将原始行复制并粘贴到新行中
【发布时间】:2017-04-20 18:30:03
【问题描述】:

我有一行包含一个单元格,该单元格指定要在下面插入的行数。我的那部分代码运行良好。然后我想获取原始行的内容并粘贴到新创建的行中,然后从这些行中的特定单元格中删除信息。那就是我遇到问题的地方。这是我的代码:

Set ws = ActiveSheet

Dim rw, num As Long

rw = 5

While ws.Cells(rw, 16).Value <> ""
    num = ws.Cells(rw, 16).Value

    If num = 0 Then
        rw = rw + 1
    Else

        Range(Cells(rw + 1, 16), Cells(rw + num, 16)).EntireRow.Insert shift:=xlDown
        Rows(rw).Select
        Selection.Copy
        Range(Rows(rw + 1), Rows(rw + num)).Paste
        Range(Cells(rw + 1, 9), Cells(rw + num, 9)).ClearContents
        rw = rw + num + 1
    End If
Wend

End Sub

我不明白为什么我不能将原始行内容粘贴到我新创建的行中 原始行被复制并在我的 ms 剪贴板上但不粘贴。我尝试过使用 Range().Paste、Rows().Paste、Cells().Paste 以及这三者的组合,但到目前为止没有任何效果。非常感谢任何帮助,谢谢。

【问题讨论】:

    标签: vba excel loops while-loop


    【解决方案1】:

    你可以试试

    范围(行(rw + 1),行(rw + num))。PasteSpecial xlPasteValues Application.CutCopyMode = False

    范围(行(rw + 1),行(rw + num))。PasteSpecial 粘贴:=xlPasteAll,_ 操作:=xlNone,SkipBlanks:=True,转置:=False Application.CutCopyMode = False

    【讨论】:

      【解决方案2】:

      我会使用 @pascalbaro 所写的内容,但也会摆脱 select 语句。此外,如果这是您唯一的子程序,您可能希望限制屏幕更新。如果不只是将它们添加到调用它们的那个中。

      Application.ScreenUpdating = False
      Set ws = ActiveSheet
      Dim rw, num As Long
      rw = 5
      
      While ws.Cells(rw, 16).Value <> ""
          num = ws.Cells(rw, 16).Value
      
          If num = 0 Then
              rw = rw + 1
          Else
      
              Range(Cells(rw + 1, 16), Cells(rw + num, 16)).EntireRow.Insert shift:=xlDown
      
              'remove the selection statements
              Rows(rw).Copy
      
              Range(Rows(rw + 1), Rows(rw + num)).PasteSpecial Paste:=xlPasteAll, _
                  Operation:=xlNone, SkipBlanks:=True, Transpose:=False
      
              Range(Cells(rw + 1, 9), Cells(rw + num, 9)).ClearContents
      
              rw = rw + num + 1
      
          Application.CutCopyMode = False
      
          End If
      Wend
      
      Application.ScreenUpdating = True
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-05
        • 2017-10-12
        • 1970-01-01
        相关资源
        最近更新 更多