How can i copy excell data from a sheet by parts?

zabiullakhan

Active Member
Joined
Aug 30, 2010
Messages
310
I want to copy excell data in parts for example 10 rows at a time.
  • I need to determin the no of rows present in the sheet1
  • Copy 10 rows at a time from sheet1 and past into sheet2 (using a loop)
Note : I cant copy the whole data at once as per the requirment.

Please help.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
You can find the last used row by using the End method from the bottom of the sheet.
Then copy rows using something like...
ActiveCell.Resize(10,1).EntireRow.Copy

To paste into Sheet2, find the last used row in that sheet and then paste into the next row.

Then you can offset by 10 rows, and repeat. Stop the loop when ActiveCell.Row is greater than the last row with content.

Denis
 
Upvote 0
Code:
Sub Extract10Rows()
    
    Dim i As Long, sh As Worksheet
    
    Set sh = Worksheets("Sheet2")
    
    For i = 1 To Range("A1").End(xlDown).Rows.Count
        If i Mod 10 = 0 Then
            Range(Cells(i, 1), Cells(i - 9, 1)).Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row+1)
        End If
    Next

End Sub
 
Last edited:
Upvote 0
The below code dosent work as it dosent satisfys the condition in red
Code:
Sub Extract10Rows()
    
    Dim i As Long, sh As Worksheet
    
    Set sh = Worksheets("Sheet2")
    
    For i = 1 To Range("A1").End(xlDown).Rows.Count
       [COLOR=red] If i Mod 10 = 0 Then
[/COLOR]            Range(Cells(i, 1), Cells(i - 9, 1)).Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row+1)
        End If
    Next

End Sub

however i wan to copy all the rows for example

Total no of rows = 34

loop1 = 10 -> total 10 (start from 1st row to 10th row)
loop2 = 10 -> total 20 (start from 11th row to 20th row)
loop3 = 10 -> total 30 (start from 21st row to 30th row)
loop4 = 4 -> total 34 (start from 31st row to 34th row)

Please help
 
Upvote 0
Try this one:
Code:
Sub Extract10Rows()
    
    Dim lRows As Long, lLast As lost
    Dim i As Long, sh As Worksheet
    
    lRows = Range("A1").End(xlDown).Rows.Count
    Set sh = Worksheets("Sheet2")
    
    For i = 1 To lRows
        If i Mod 10 = 0 Then
            lLast = i
            Range(Cells(i, 1), Cells(i - 9, 1)).Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0)
        ElseIf IsEmpty(Cells(i + 1, 1)) Then
            Range(Cells(i, 1), Cells(lRows - lLast, 1)).Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0)
        End If
    Next

End Sub
 
Upvote 0
No the loop dosent work as lRows has value 1

the code below is not giving us the total no of rows.. hence loop exits for the first time itself.

Code:
lRows = Range("A1").End(xlDown).Rows.Count
 
Upvote 0
Code:
lRows = Range("A1").End(xlDown).Row
 
Upvote 0
Code:
Sub Extract10Rows()
    
    Dim lRows As Long, lLast As lost
    Dim i As Long, sh As Worksheet
    
    lRows = Range("A1").End(xlDown).Rows
    Set sh = Worksheets("Sheet2")
    
    For i = 1 To lRows
        If i Mod 10 = 0 Then
            lLast = i
            Range(Cells(i, 1), Cells(i - 9, 1)).[B][COLOR="Red"]EntireRow[/COLOR][/B].Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0)
        ElseIf IsEmpty(Cells(i + 1, 1)) Then
            Range(Cells(i, 1), Cells(lRows - lLast, 1)).[B][COLOR="red"]EntireRow[/COLOR][/B].Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0)
        End If
    Next

End Sub
 
Upvote 0
code seem's to be going wrong in last loope, ifelse statement.

Code:
ElseIf IsEmpty(Cells(i + 1, 1)) Then
            Range(Cells(i, 1), Cells(lRows - lLast, 1)).EntireRow.Copy Destination:=sh.Range("A" & sh.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0)
        End If

It copys the data 25 times
 
Upvote 0

Forum statistics

Threads
1,224,562
Messages
6,179,526
Members
452,923
Latest member
JackiG

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