2016-02-08 158 views
0

我想複製和粘貼動態範圍。特別是對每個複製範圍源粘貼重複值。下面是代碼,我從錄製宏創建:複製動態範圍並粘貼具有重複值的動態範圍

Sub copyRange() 
Range("A2").Select 
Range(Selection, Selection.End(xlToRight)).Select 
Selection.Copy 
Range("L2:S7").Select 
ActiveSheet.Paste 

Range("A3").Select 
Range(Selection, Selection.End(xlToRight)).Select 
Application.CutCopyMode = False 
Selection.Copy 
Range("L8:S13").Select 
ActiveSheet.Paste 

Range("A4").Select 
Range(Selection, Selection.End(xlToRight)).Select 
Application.CutCopyMode = False 
Selection.Copy 
Range("L14:S19").Select 
ActiveSheet.Paste 

Range("A5").Select 
Range(Selection, Selection.End(xlToRight)).Select 
Application.CutCopyMode = False 
Selection.Copy 
ActiveWindow.SmallScroll Down:=3 
Range("L20:S25").Select 
ActiveSheet.Paste 
End Sub 

這是截圖輸出,我想:

enter image description here

+0

感謝Prasar Khode改正我的queston –

回答

1
Sub CopyPasteData() 
Dim lRw As Long, lRw_2 As Long, x As Long, rActive As Range 

Set rActive = ActiveCell 
lRw = Cells(Rows.Count, "A").End(xlUp).Row 

Application.ScreenUpdating = False 

Range("K2:R" & Rows.Count).ClearContents 

For i = 2 To lRw 
    x = x + 1 
    Range("A" & i & ":H" & i).Copy 
    lRw_2 = Cells(Rows.Count, "K").End(xlUp).Row + 1 
    With Range("K" & lRw_2).Resize(6) 
     .PasteSpecial xlPasteAll 
     .Offset(, -1).Value = x 
    End With 
Next i 

Application.CutCopyMode = False 
rActive.Select 
Application.ScreenUpdating = True 

End Sub 
+0

非常感謝Sixthense它的工作,但是當我再次運行Sub CopyPasteData,或者在所有源拷貝範圍之後重複拷貝數據到範圍目標。我只需要一次,而不是在運行該子時重複。很多謝謝你的幫助。 –

+0

在上面的代碼中添加範圍(「K2:R」和Rows.Count).ClearContents行以清除舊條目。 – Sixthsense

+0

是的..謝謝你的工作......很多感謝。 :-) –