Select each nth row in Excel - Super User most recent 30 from superuser.com2010-03-22T02:04:24Zhttp://superuser.com/feeds/question/69230http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://superuser.com/questions/69230/select-each-nth-row-in-excel2Select each nth row in ExcelPatrick McElhaneyhttp://superuser.com/users/8602009-11-11T22:10:59Z2009-11-12T20:15:04Z
<p>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.)</p>
<p>What's the easiest way to do that in Excel 2003?</p>
<p>See also: <a href="http://superuser.com/questions/69513/select-each-nth-row-in-numbers">Select each nth row in Numbers</a></p>
http://superuser.com/questions/69230/select-each-nth-row-in-excel/69231#692312Answer by Randolph Potter for Select each nth row in ExcelRandolph Potterhttp://superuser.com/users/34632009-11-11T22:25:47Z2009-11-11T22:25:47Z<p>Personally, I'd record a macro to do the first three or four lines (at 7-row increments, of course), and copy the lines to a new sheet. Then I'd edit the macro to use a loop that counts to the number of populated rows in the sheet, with a step of 7.</p>
<p>Pseudo-code example:</p>
<pre><code>Dim i as Integer
For i = 1 To 1000 Step 7
'Add current row to selection
...
Next i
'Copy the selected rows to new sheet
...
</code></pre>
http://superuser.com/questions/69230/select-each-nth-row-in-excel/69232#692323Answer by tpierzina for Select each nth row in Exceltpierzinahttp://superuser.com/users/176662009-11-11T22:27:52Z2009-11-12T20:15:04Z<ol>
<li>Insert a column </li>
<li>In first row insert formula =MOD(ROW(),7)</li>
<li>Copy down</li>
<li>Copy/paste special/values</li>
<li>Data/Filter out the ones you want (0 or 6, probably)</li>
<li>Delete the rest of the rows Remove filter Delete column</li>
</ol>
http://superuser.com/questions/69230/select-each-nth-row-in-excel/69278#692783Answer by DaveParillo for Select each nth row in ExcelDaveParillohttp://superuser.com/users/105542009-11-12T00:42:01Z2009-11-12T00:42:01Z<p>Really just finishing the idea Randolph Potter started....</p>
<p>For the record, I don't thing you could ever come up with this by recording. macro record is a good way to familiarize yourself with the Excel Object Model, but a not very good way to write reusable functions.</p>
<pre><code>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
</code></pre>