Macro to copy rows from a sheet to another based on a criteria

JayCar

New Member
Joined
Feb 13, 2019
Messages
10
Hi there,

I have limited programming knowledge but I manage to créate this code below. Rows from sheet1 are copied in sheet2 if column E is "YES". This Works but only If I run the Macro in sheet 1.Anybody knows why?
Code:
'Sub CopyRowToSheet2()
'========================================================================
' COPIES ALL ROWS WHERE COLUMN E HAS VALUE "YES" FROM MainSheet
' INTO "SHEET2" SHEET
'========================================================================
    Worksheets("Sheet2").Range("A2:E1000").Clear
    Dim LastRowSheet1, LastRowSheet2 As Long
    Dim i As Long
    Application.ScreenUpdating = False
'    LastRowSheet2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
'    Sheets("Sheet2").Range("A2:E" & LastRowSheet2).ClearContents
    LastRowSheet1 = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    With Worksheets("Sheet1")
        For i = 2 To LastRowSheet1 Step 1
            If Cells(i, "E").Value = "YES" Then
                LastRowSheet2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
                Rows(i).Copy Worksheets("Sheet2").Range("A" & LastRowSheet2 + 1)
            End If
        Next i
    End With
    Application.ScreenUpdating = True
    Sheet3.Select
    
End Sub
 
Last edited by a moderator:
Change this
Code:
.Range("A" & i).Resize(, 3).Copy Worksheets("Sheet2").Range("B" & LastRowSheet2 + 1)
to
Code:
.Range("A" & i).Resize(, 3).Copy 
Worksheets("Sheet2").Range("B" & LastRowSheet2 + 1).PasteSpecial xlPasteValues
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Thanks! I´m trying to do the same for complete rows - from sheet1 to sheet3 -, as below:

# For i = 2 To LastRowSheet1 Step 1
LastRowSheet3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
.Rows(i).Copy Worksheets("Sheet3").Range("A" & LastRowSheet3 + 1).PasteSpecial xlPastevalues
Next i#

But is giving a compile error "Expected: end of statement" . Not sure what´s going on here...
 
Upvote 0
Can you please post the entire code & indicate which line is giving the error.

Also you do not key in a # symbol, you click the # icon in the reply window & paste the code between the tags that appear.
 
Upvote 0
Hi! Sorry for not copying the code in the right manner.See below de code:

Code:
Sub SummaryTable()
'========================================================================
' COPIES ALL SHEETS TO THE SUMMARY SHEET
'========================================================================
    Worksheets("Sheet3").Range("A2:u1000").Clear
    Dim LastRowSheet1, LastRowSheet2, LastRowSheet4, LastRowSheet3 As Long
    Dim i As Long
    Application.ScreenUpdating = False
'    LastRowSheet3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
'    Sheets("Sheet3").Range("A2:u" & LastRowSheet3).ClearContents
    LastRowSheet1 = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    With Worksheets("Sheet1")
        For i = 2 To LastRowSheet1 Step 1
                LastRowSheet3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
                .Rows(i).Copy Worksheets("Sheet3").Range("A" & LastRowSheet3 + 1).PasteSpecial xlPasteValues
            :=False, Transpose:=False
        Next i
    End With
    LastRowSheet2 = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    With Worksheets("Sheet2")
        For i = 2 To LastRowSheet2 Step 1
                LastRowSheet3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
                .Rows(i).Copy Worksheets("Sheet3").Range("A" & LastRowSheet3 + 1).PasteSpecial xlPasteValues
        Next i
    End With
    LastRowSheet4 = Worksheets("Sheet4").Range("A" & Rows.Count).End(xlUp).Row
    With Worksheets("Sheet4")
        For i = 2 To LastRowSheet4 Step 1
                LastRowSheet3 = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Row
                .Rows(i).Copy Worksheets("Sheet3").Range("A" & LastRowSheet3 + 1).PasteSpecial xlPasteValues
        Next i
    End With

The error is related to line:

Code:
 .Rows(i).Copy Worksheets("Sheet3").Range("A" & LastRowSheet3 + 1).PasteSpecial xlPasteValues

Compile error: Expected end of statement.
 
Upvote 0
You need to remove this line
Code:
 :=False, Transpose:=False
 
Upvote 0
You need to remove this from each of those lines
Code:
.PasteSpecial xlPasteValues
Unless you want values only in which case it should be
Code:
                .Rows(i).Copy
                Worksheets("Sheet3").Range("A" & LastRowSheet3 + 1).PasteSpecial xlPasteValues
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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