Need a copy macro from one sheet to another based on a criteria

profklein

New Member
Joined
May 20, 2017
Messages
20
Hello again.

I want to copy only some of the rows of data from one sheet to another. I want to have a macro in sheet y
that will look at the data in sheet x and copy only those rows that have a "YES" in, say, column AA.
The macro should start looking in row 2 in sheet x and starting copying in row 2 in sheet y.

Thank you to all who have helped me so far and all who will help me in the future on my path to a docorate.


Gene
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try:
Code:
Sub copyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("x")
    Set desWS = Sheets("y")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    srcWS.Range("AA1:AA" & LastRow).AutoFilter Field:=1, Criteria1:="YES"
    srcWS.Range("AA2:AA" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy desWS.Cells(2, 1)
    srcWS.Range("AA1").AutoFilter
    Application.ScreenUpdating = True
End Sub
The macro assumes you have headers in row 1 and the data starts in row 2.
 
Last edited:
Upvote 0
I am trying to change the macro in two respects:
1) Instead of the criteria being the cell in column AA needs to be YES, The cell in column BG should NOT be 58
2) The macro should start on row 3 and not row 2

Thanks
Gene
 
Upvote 0
The macro assumes you have headers in row 2.
Code:
Sub copyRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet
    Set srcWS = Sheets("x")
    Set desWS = Sheets("y")
    LastRow = srcWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    srcWS.Range("BG2:BG" & LastRow).AutoFilter Field:=1, Criteria1:="<>58"
    srcWS.Range("BG3:BG" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Copy desWS.Cells(2, 1)
    srcWS.Range("AA1").AutoFilter
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,198
Members
448,554
Latest member
Gleisner2

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