VBA - Copy and paste entire row to second sheet based on cell value

cwdamron

New Member
Joined
Jul 11, 2013
Messages
9
Hello,

Today I finally taught myself a basic understanding of macros/VBA. I've made a lot of progress on my project, but am stuck at the moment...Here's what's going on:

I have a sheet titled "All Trades" that contains the raw data like the example below. I have two other sheets titled "As-Of Trades" and "Non As-Of Trades". I'm needing a code to copy the entire row of data from the "All Trades" sheet, and paste it in the next available row on the other two sheets, based on the value of YES or NO.

If Yes - Copy row to sheet titled "As-Of Trades"
If No - Copy row to sheet titled "Non As-Of Trades"

Any help is MUCH appreciated THANK YOU!!

FundAccountAmountGain/LossAs/Of? (Y/N)
111111$15000.00-$1.51YES
122222$32158.52$78.14YES
2123123$1.00$0.00NO

<TBODY>
</TBODY>
 
VBA Code:
Sub MM1()
Dim lr As Long, lr2 As Long, r As Long
lr = Sheets("All Trades").Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("As-Of-Trades").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("E" & r).Value = "YES" Then
        Rows(r).Copy Destination:=Sheets("As-Of-Trades").Range("A" & lr2 + 1)
        lr2 = Sheets("As-Of-Trades").Cells(Rows.Count, "A").End(xlUp).Row
    with Sheets("As-Of-Trades").Range("A" & lr2 + 1)
        .value=.value
    end with  
End If
Next r
End Sub

Thanks for getting back @Michael M does the code work for you? It's giving me the same result.

Allow me to elaborate a bit futher. Looking at the attached screenshot I have sheet 'src'. This contains raw data (say a data download from a system). In 'All Trades' I enrich the data with other fields and then records with 'YES' values in column E then get copied to sheet 'As-of-Trades' as values (not formula).

It's almost like we need some PasteSpecial xlPasteValues function but I don't understand VBA enough to achieve this.
 

Attachments

  • data_split.png
    data_split.png
    2.1 KB · Views: 15
Upvote 0

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Try changing to
VBA Code:
Sub MM1()
Dim lr As Long, lr2 As Long, r As Long
lr = Sheets("All Trades").Cells(Rows.Count, "A").End(xlUp).Row
lr2 = Sheets("As-Of-Trades").Cells(Rows.Count, "A").End(xlUp).Row
For r = lr To 2 Step -1
    If Range("E" & r).Value = "YES" Then
        Rows(r).Copy Destination:=Sheets("As-Of-Trades").Range("A" & lr2 + 1)
       With Sheets("As-Of-Trades").Rows(r2 + 1)
        .Value = .Value
    End With
        lr2 = Sheets("As-Of-Trades").Cells(Rows.Count, "A").End(xlUp).Row
End If
Next r
End Sub
 
Last edited:
Upvote 0
I found some alternative code that I could successfully adopt. All good now.

VBA Code:
Public Sub CopyRows()
    Application.ScreenUpdating = False
    Worksheets("Sheet1").Range("A2:H200").Clear
    Sheets("Consol").Select
    ' Find the last row of data
    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    ' Loop through each row
    For x = 2 To FinalRow
        ' Decide if to copy based on column I
        ThisValue = Cells(x, 9).Value
        
        If ThisValue = "ToCorecon" Then
            Cells(x, 1).Resize(1, 8).Copy
            Sheets("Sheet1").Select
            NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
            Cells(NextRow, 1).Select
            
            ' Paste values only
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
            '
            
            Sheets("Consol").Select
          
        End If
    Next x
    
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,463
Messages
6,124,965
Members
449,201
Latest member
Jamil ahmed

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