2016-04-21 96 views
0

我正在使用此宏將一張紙複製到另一張紙上,但每次運行宏時它都會覆蓋數據。有什麼方法可以更改宏,以便它粘貼的任何數據在下一個空白行中?宏複製到另一張紙上的下一個空白行

謝謝:)

Sub CopyYes() 
Dim c As Range 
Dim j As Integer 
Dim Source As Worksheet 
Dim Target As Worksheet 

' Change worksheet designations as needed 
Set Source = ActiveWorkbook.Worksheets("Main Data") 
Set Target = ActiveWorkbook.Worksheets("Cheque Data") 

j = 1  ' Start copying to row 1 in target sheet 
For Each c In Source.Range("A1:A1000") ' Do 1000 rows 
    If c = "Cheque" Then 
     Source.Rows(c.Row).Copy Target.Rows(j).End(xlUp).Offset(1) 
     j = j + 1 
    End If 
Next c 

    ' Change worksheet designations as needed 
Set Source = ActiveWorkbook.Worksheets("Main Data") 
Set Target = ActiveWorkbook.Worksheets("Gift Card Data") 

j = 1  ' Start copying to row 1 in target sheet 
For Each c In Source.Range("A1:A1000") ' Do 1000 rows 
    If c = "Gift Card" Then 
     Source.Rows(c.Row).Copy Target.Rows(j).End(xlUp).Offset(1) 
     j = j + 1 
    End If 
Next c 

    ' Change worksheet designations as needed 
Set Source = ActiveWorkbook.Worksheets("Main Data") 
Set Target = ActiveWorkbook.Worksheets("Promo Code Data") 

j = 1  ' Start copying to row 1 in target sheet 
For Each c In Source.Range("A1:A1000") ' Do 1000 rows 
    If c = "Promo Code" Then 
     Source.Rows(c.Row).Copy Target.Rows(j).End(xlUp).Offset(1) 
     j = j + 1 
    End If 
Next c 

Sheets("Main Data").Range("A2:F200").ClearContents 
Sheets("Main Data").Range("J2:Q200").ClearContents 

末次

回答

2

每個j=1之前添加

lastrow = Target.Range("A65000").End(xlUp).Row + 1 

,並更改j = 1j = lastrow

+0

這是工作,非常感謝:) – Wsirhc

相關問題