2013-02-11 147 views
0

我有此代码(工作)。将第n行复制并粘贴到其他表格(mod)

Sub Copy_Ten() 
Dim X As Long, LastRow As Long 
Dim CopyRange As Range 
LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row 
For X = 1 To LastRow Step 4 
    If CopyRange Is Nothing Then 
     Set CopyRange = Rows(X).EntireRow 
    Else 
     Set CopyRange = Union(CopyRange, Rows(X).EntireRow) 
    End If 
Next 
If Not CopyRange Is Nothing Then 
CopyRange.Copy Destination:=Sheets("Sheet2").Range("A1") 
End If 
End Sub 

在工作表2上始终从A1开始。我希望它能够继续寻找下一个空间。

我的代码是Range("A1").End(xldown).Select但是我不知道该把它放在哪里。

因此,最终表2不会在第一次从A1开始......因为会有越来越多的列表。

回答

1

您可以使用代码,但它包装在一个与功能,像这样

With Sheets("Sheet2") 
    lastRow2 = .Cells(.Rows.Count, "A").End(xlUp).Row 
End With 

然后改变

CopyRange.Copy Destination:=Sheets("Sheet2").Range("A1") 

CopyRange.Copy Destination:=Sheets("Sheet2").Range("A" & lastRow2) 

为了使这更清楚一点尝试以下

Sub Copy_Ten() 
    Dim X As Long, LastRow As Long, PasteRow As Long 
    Dim CopyRange As Range 
    LastRow = Cells(Cells.Rows.Count, "A").End(xlUp).Row 
    With Sheets("Sheet2") 
     PasteRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
    End With 
    For X = 1 To LastRow Step 4 
     If CopyRange Is Nothing Then 
      Set CopyRange = Rows(X).EntireRow 
     Else 
      Set CopyRange = Union(CopyRange, Rows(X).EntireRow) 
     End If 
    Next 
    If Not CopyRange Is Nothing Then 
     CopyRange.Copy Destination:=Sheets("Sheet2").Range("A" & PasteRow) 
    End If 
End Sub 
+0

所以它应该看起来像这样。 Dim Copy As Long,LastRow As Long Dim CopyRange As Range With Sheets(“Sheet2”) lastRow2 = .Cells(.Rows.Count,“A”)。End (xlUp).Row 完随着 对于x = 1至LASTROW步骤4 如果copyRange是是Nothing然后 集copyRange是=行(X).EntireRow 否则 集copyRange是=联盟(copyRange是,行(X).EntireRow) 结束如果 接着 如果不copyRange是是Nothing然后 CopyRange.Copy目的地:=表( “Sheet 2中”)范围( “A” &lastRow2) 结束如果结束 子。 ' – Arthor 2013-02-11 01:27:32

+1

我已更新我的答案以提供完整的代码。 – Rick 2013-02-11 01:41:18

+0

加了一个genuies,一个genuies – Arthor 2013-02-11 01:42:38