2017-04-20 79 views
0

我有一行指定要在其下插入的行数的单元格。我的代码的那部分工作正常。然后我想获取原始行的内容并粘贴到新创建的行中,然后删除这些行中特定单元格的信息。那是我遇到问题的地方。这里是我的代码:创建行,然后将原始行复制并粘贴到新行

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()。粘贴和三个组合,迄今没有任何工作。任何帮助非常感谢,谢谢。

回答

0

您可以尝试

 

    Range(Rows(rw + 1), Rows(rw + num)).PasteSpecial xlPasteValues 
    Application.CutCopyMode = False 

 

    Range(Rows(rw + 1), Rows(rw + num)).PasteSpecial Paste:=xlPasteAll, _ 
      Operation:=xlNone, SkipBlanks:=True, Transpose:=False 
     Application.CutCopyMode = False 

0

我会用什么@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 
相关问题