在excel中复制每第n行

时间:2014-10-31 18:14:17

标签: excel excel-vba optimization excel-formula excel-2003 vba

例如,如果我有30行,并且我想要复制第1行,第2行和第3行,则跳过5行,然后复制接下来的3行,然后再跳过5行并复制接下来的3行:我该怎么做?到目前为止我只有这个公式

=OFFSET(Sheet1!$A$1,(ROW()-1)*5,0)

只获取一个单元格值,但我希望将整行复制到另一个工作表中。

任何帮助都会很棒!感谢

2 个答案:

答案 0 :(得分:-1)

Here is a hint: 
Row Number Modulus 8 in (1,2,3) identifies the target Row Numbers.


Enter in first column, first row and copy down for each column:
=IF(AND(MOD(ROW(Sheet1!A1),8) > 0,MOD(ROW(Sheet1!A1),8) < 4),Sheet1!A1,"")

答案 1 :(得分:-2)

这样就可以了。只需使用Cells(行#,Col#)作为循环。

Sub Copy3Skip5()

Dim iRow As Integer
Dim count As Integer
Dim tRow As Integer
Dim lastRow As Integer
Dim source As String
Dim target As String

source = "Sheet1"
target = "Sheet2"
'Get the last row on your source sheet.
lastRow = Sheets(source).Range("A65536").End(xlUp).Row
'Set the Target Sheet first row
tRow = 2
'assuming your data starts on Row 2 after a header row.
'Loop through the Source sheet
For iRow = 2 To lastRow
    count = 1
    Do While count <= 3
        'Cells(tRow, 1) translates to Range("A2") if tRow = 2  and "A3" if tRow = 3. and so on
        Sheets(target).Cells(tRow, 1) = Sheets(source).Cells(iRow, 1)
        tRow = tRow + 1
        iRow = iRow + 1
        count = count + 1
    Loop
'add 4 to the iRow, because we already added 1 at the end of the previous loop
    iRow = iRow + 4
Next iRow

End Sub

Sheet1 Result