Hi folks,
I got the below code online somewhere a a few months back and its recently come in handy for something I'm doing (copying every other row and pasting it elsewhere).
However, I'm new to VBA and do not understand 1 thing (which is a problem since I'll need to troubleshoot): why does "Set RowSelect =" appear twice??
I can't wrap my head around why it's Set as MyRange.Rows(2), but then 2 lines later its Set as something else, but in the end it all works perfectly. Don't you Set a variable to an object once and be done with it?
Any explanation on that piece would be super appreciated. Cheers, James.
Sub Copy_every_nth_row()
Dim MyRange As Range, RowSelect As Range, i As Integer, LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Set MyRange = Range("A1:A" & LR)
Set RowSelect = MyRange.Rows(2)
For i = 2 To LR Step 2
Set RowSelect = Union(RowSelect, MyRange.Rows(i))
Next i
RowSelect.Copy Range("D2")
End Sub
I got the below code online somewhere a a few months back and its recently come in handy for something I'm doing (copying every other row and pasting it elsewhere).
However, I'm new to VBA and do not understand 1 thing (which is a problem since I'll need to troubleshoot): why does "Set RowSelect =" appear twice??
I can't wrap my head around why it's Set as MyRange.Rows(2), but then 2 lines later its Set as something else, but in the end it all works perfectly. Don't you Set a variable to an object once and be done with it?
Any explanation on that piece would be super appreciated. Cheers, James.
Sub Copy_every_nth_row()
Dim MyRange As Range, RowSelect As Range, i As Integer, LR As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
Set MyRange = Range("A1:A" & LR)
Set RowSelect = MyRange.Rows(2)
For i = 2 To LR Step 2
Set RowSelect = Union(RowSelect, MyRange.Rows(i))
Next i
RowSelect.Copy Range("D2")
End Sub