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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
11,726
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
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:

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
11,726
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
You are very welcome. :)
 

profklein

New Member
Joined
May 20, 2017
Messages
20
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
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
11,726
Office Version
  1. 2013
  2. 2010
Platform
  1. Windows
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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,117
Messages
5,835,489
Members
430,358
Latest member
zzc1128

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
Top