Copy If And

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
Not sure what I am doing wrong. Any assistance or suggestions would be great..

Thank You

Trying to copy to another worksheet if meets two criteria. Code I am using is;

Application.ScreenUpdating = False

lr = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

For w = 2 To lr
If Worksheets("Sheet1").Cells(w, 3).Value = "Green" And Cells(w, 5).Value = "No" Then
Worksheets("Sheet1").Rows(w).Copy

ThisWorkbook.Sheets.Add(after:=ThisWorkbook.Sheets(Sheets.Count)).Name = "Test"


lr2 = Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Test").Cells(lr2 + 1, 1).PasteSpecial

End If
Next w

Application.ScreenUpdating = True

End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Code:
Sub billandrew()

Dim lr As Long, lr2 As Long
Dim aWS As Worksheet

Application.ScreenUpdating = False

Set aWS = ActiveSheet
Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Test"
aWS.Activate

lr = Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row
lr2 = 1

 For W = 2 To lr
    If Worksheets("Sheet1").Cells(W, 3).Value = "Green" And Cells(W, 5).Value = "No" Then
       
        Worksheets("Test").Rows(lr2).Value = Worksheets("Sheet1").Rows(W).Value
        lr2 = lr2 + 1
 
    End If
 Next W

 Application.ScreenUpdating = True
 End Sub
 
Upvote 0
You had a copy with no destination to paste. your add worksheet wasn't set up correctly and if it was, inside your loop, it would have thrown an error with every iteration as it would have repeated on every true result. You didn't need to find the last row of lr2 because you are trying to find the last row of a new worksheet=1, then append to every True iteration.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,631
Latest member
a_potato

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