Looping a select rows

andre5

Board Regular
Joined
Aug 11, 2005
Messages
108
Hi

I need to create a loop which will select 4 rows copy and paste it to a new sheet. then go to the Next 4 rows and copy them to a new sheet. I need this to happen untill the row is blank (empty).

Any idea how to do this loop?
 

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hello, Andre,

try this
Code:
Option Explicit

Sub copy_rows_to_new_sheets()
'Erik Van Geit
'051215

Dim LR As Long
Dim I As Long
Dim SourceSheet As Worksheet

'edit only these lines
Const FR = 2                'first row
Const GroupRows = 4         'number of rows whiwh will stay together
Set SourceSheet = Sheets(1)

'action
LR = Cells.Find("*", [A1], xlFormulas, xlPart, xlByRows, xlPrevious, False, False).Row

Application.ScreenUpdating = False
    For I = FR To LR Step GroupRows
    SourceSheet.Rows(I & ":" & I + GroupRows - 1).Copy
    Sheets.Add
    ActiveSheet.Paste
    ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues
    Next I
Application.ScreenUpdating = True
End Sub
REMARQUE
if there are many rows you could run into problems adding so many sheets
instead of
LR = Cells.Find....
you could use a "hardcoded" number
LR = 27 or whatever

kind regards,
Erik
 

Oorang

Well-known Member
Joined
Mar 4, 2005
Messages
2,071
This will do what you asked, but why don't you elaborate on what you are trying to do and why you selected this way to do it? I think their is a more effecient way.


<hr>
<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> CopyRows()
Sheets("Sheet4").Select
Range("A1").Select
Sheets("Sheet3").Select
Range("A1").Select
<SPAN style="color:#00007F">Do</SPAN> <SPAN style="color:#00007F">Until</SPAN> Application.WorksheetFunction.CountA(Rows(ActiveCell.Row & ":" & ActiveCell.Offset(3, 0).Row)) = 0
Rows(ActiveCell.Row & ":" & ActiveCell.Offset(3, 0).Row).Copy
Sheets("Sheet4").Select
ActiveSheet.Paste
ActiveCell.Offset(4, 0).Select
Sheets("Sheet3").Select
ActiveCell.Offset(4, 0).Select
<SPAN style="color:#00007F">Loop</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
<hr>
 

andre5

Board Regular
Joined
Aug 11, 2005
Messages
108
WoW Thank you

Erik your VB runs 100% Thank you!!!!!!

:pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray:
 

Watch MrExcel Video

Forum statistics

Threads
1,118,824
Messages
5,574,525
Members
412,601
Latest member
TheBeaniacExpress
Top