【问题标题】:Copying every nth Rows in excel复制excel中的每n行
【发布时间】:2014-10-31 18:14:16
【问题描述】:

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

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

这只会给我一个单元格值,但我希望将整行复制到另一张表中。

任何帮助都会很棒!谢谢

【问题讨论】:

  • 对不起,我没有指定,我愿意使用 VBA 或公式。我也在使用 excel 2003。这个公式被放入表 2 并向下拖动。它在所需列中每 5 个值收集一次。但是我想复制整行。

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


【解决方案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,"")

【讨论】:

  • 感谢您的回答。我还在想如何复制 3 行,跳过下第 N 行,复制 3 行,跳过下第 n 行等...
  • 他暗示了模数。将 "=MOD(ROW(A1),8)" 放入工作表的单元格 A1 并向下拖动,看看会发生什么。
【解决方案2】:

这样就可以了。只需将 Cells(row#, 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多