Home » Questions » Computers [ Ask a new question ]

Select each nth row in Excel

Select each nth row in Excel

I have an Excel spreadsheet with thousands of rows. I want to select every 7th row from that spreadsheet. (By "select," I mean delete all of the other rows, or copy the selected rows into a new worksheet.)

Asked by: Guest | Views: 445
Total answers/comments: 3
Guest [Entry]

"Insert a column
In first row insert formula =MOD(ROW(),7)
Copy down
Copy/paste special/values
Data/Filter out the ones you want (0 or 6, probably)
Delete the rest of the rows Remove filter Delete column"
Guest [Entry]

"Really just finishing the idea Randolph Potter started....

For the record, I don't think you could ever come up with this by recording. Macro recording is a good way to familiarize yourself with the Excel Object Model, but not a very good way to write reusable functions.

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"
Guest [Entry]

"for every 7th row,

Insert a column
In row 1 to row 6 put an ""X""
in row 7 put 1,
auto-fill your column with that block
Use ""Remove Duplicates"" on that column
Delete 1st row (with the 1st ""X"").
Delete added column"