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?
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
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
 
Upvote 0
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>
 
Upvote 0
WoW Thank you

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

:pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray: :pray:
 
Upvote 0

Forum statistics

Threads
1,213,521
Messages
6,114,109
Members
448,548
Latest member
harryls

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top