Copy Paste all Rows where Range cell value > 0

terrycr

New Member
Joined
Jul 14, 2014
Messages
16
I am trying to Copy all Rows (Sheet "Payouts") where Range cell value > 0, and paste to Sheet "PaymentRecords".
Right now it only copies the last row that is selected and pastes it. Where there is a value in Column B (starting at B10 and rest of column), I want to select those rows to paste to other sheet. See the image below... I would want to copy rows 11 & 14... right now it will only copy row 14.


Here is my current code:

Sub PayoutsRecordPayments()

Dim rangeB As Range
Set rangeB = Sheets("Payouts").Range("B10:B6800")
lastRow = Sheets("Payouts").Range("B" & Rows.Count).End(xlUp).Row
PRlastRow = Sheets("PaymentRecords").Range("B" & Rows.Count).End(xlUp).Row
'For Each cell In Sheets("Payouts").Range("b10:b6500" & lastRow)
For Each cell In rangeB
If cell.value > 0 Then

With Sheets("Payouts")
cell.EntireRow.Copy
End With

' This finds the last row +1 on PaymentRecords and pastes there

Sheets("PaymentRecords").Range("A" & PRlastRow + 1).PasteSpecial xlValues


End If

Next

End Sub



CopyPastePayments.PNG
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You are currently not increment the output last row counter so all the rows are copying over the previous one.

Rich (BB code):
Sub PayoutsRecordPayments()

    Dim rangeB As Range
    Dim lastrow As Long, PRlastRow As Long             ' XXX Added
 
    ' XXX Swap these 2 around
    lastrow = Sheets("Payouts").Range("B" & Rows.Count).End(xlUp).Row
    Set rangeB = Sheets("Payouts").Range("B10:B" & lastrow)
   
    PRlastRow = Sheets("PaymentRecords").Range("B" & Rows.Count).End(xlUp).Row
 
    For Each cell In rangeB
        If cell.Value > 0 Then
    
            With Sheets("Payouts")
                cell.EntireRow.Copy
            End With
        
            ' XXX Increment the last row counter
            PRlastRow = PRlastRow + 1
        
            ' This finds the last row +1 on PaymentRecords and pastes there
            Sheets("PaymentRecords").Range("A" & PRlastRow).PasteSpecial xlValues     ' XXX Removed the +1 it is included in incrementing the counter
        
        End If
 
    Next

End Sub
 
Upvote 0
Solution
You are currently not increment the output last row counter so all the rows are copying over the previous one.

Rich (BB code):
Sub PayoutsRecordPayments()

    Dim rangeB As Range
    Dim lastrow As Long, PRlastRow As Long             ' XXX Added
 
    ' XXX Swap these 2 around
    lastrow = Sheets("Payouts").Range("B" & Rows.Count).End(xlUp).Row
    Set rangeB = Sheets("Payouts").Range("B10:B" & lastrow)
  
    PRlastRow = Sheets("PaymentRecords").Range("B" & Rows.Count).End(xlUp).Row
 
    For Each cell In rangeB
        If cell.Value > 0 Then
   
            With Sheets("Payouts")
                cell.EntireRow.Copy
            End With
       
            ' XXX Increment the last row counter
            PRlastRow = PRlastRow + 1
       
            ' This finds the last row +1 on PaymentRecords and pastes there
            Sheets("PaymentRecords").Range("A" & PRlastRow).PasteSpecial xlValues     ' XXX Removed the +1 it is included in incrementing the counter
       
        End If
 
    Next

End Sub
Perfect! Thanks so much.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

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