ランドルフ・ポッターが始めたアイデアを本当に完成させたところだ….
念のために言っておきますが、記録することでこれを思いつくことはないと思います。マクロの記録は、Excelオブジェクトモデルに慣れるための良い方法ですが、再利用可能な関数を書くにはあまり良い方法ではありません。
Option Explicit
'A simple test that copies every 7th row from the active sheet to a new sheet.
Sub SimpleTest()
Dim r As Range
Dim ws As Worksheet
Set r = GetEveryNthRow(7)
If Not r Is Nothing Then
Set ws = Worksheets.Add(Before:=Sheets(1))
r.Copy ws.Range("A1")
Else
MsgBox "Nothing came back from GetEveryNthRow"
End If
Set ws = Nothing
Set r = Nothing
End Sub
'
Function GetEveryNthRow(ByVal NthRow As Long) As Range
Dim keepRows As Range
Dim r As Range
If NthRow > 0 Then
Set keepRows = Rows(1)
For Each r In ActiveSheet.UsedRange.Rows
If (r.Row Mod NthRow) = 0 Then
Set keepRows = Union(keepRows, Rows(r.Row))
End If
Next r
Set GetEveryNthRow = keepRows
Else
MsgBox "The row multiple provided must be greater than 0"
End If
Set keepRows = Nothing
End Function