Copy entire row if cell contains specific value

Phily50

New Member
Joined
Jul 8, 2013
Messages
28
Office Version
  1. 365
Platform
  1. Windows
So I have a workbook with three sheets.

Master - this contains the full data
Destroyed and Retained

Within Master there is a column (G) that has a "Y" to indicate that the file has been destroyed and an "N" to indicate that the file is retained. Currently some are blank but these will be changed as and when the documents are reviewed. There will also be further data added to the master sheet as time goes on.

The headings in all three sheets will be the same

What I am after is either a code or a formula that scans the master sheet and copies the entire row (columns A to M) into destroyed and retained dependant on column G (the Y/N). I would need this to either update dynamically or through the push of a button prior to exiting. Currently I'm manually adding them b y copy pasting and then sorting them by date, its rather laborious and repetitive!

I had a go using FILTER but couldn't work it out.

Grateful of any assistance.
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
First things first.
If you get in the habit of sheet naming sometimes capitalized and other times not, you'll get errors in your code (see Post #1)
 
Upvote 0
First things first.
If you get in the habit of sheet naming sometimes capitalized and other times not, you'll get errors in your code (see Post #1)
:confused: Have you posted in the wrong thread?

@Phily50
See If this would help.

Phily50.xlsm
ABCDEFGHIJKLM
1Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6DestroyedHdr8Hdr9Hdr10Hdr11Hdr12Hdr13
2Data 67Data 35Data 31Data 6Data 16Data 10YData 80Data 49Data 90Data 47Data 64Data 25
3Data 13Data 90Data 56Data 39Data 5Data 2NData 57Data 20Data 53Data 36Data 18Data 42
4Data 16Data 71Data 31Data 14Data 38Data 52Data 8Data 11Data 84Data 70Data 81Data 15
5Data 66Data 21Data 99Data 68Data 47Data 41NData 15Data 99Data 89Data 35Data 3Data 29
6Data 76Data 25Data 81Data 76Data 36Data 90YData 33Data 20Data 64Data 62Data 71Data 46
7
Master


Formula needs to go in A2 only

Phily50.xlsm
ABCDEFGHIJKLM
1Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6DestroyedHdr8Hdr9Hdr10Hdr11Hdr12Hdr13
2Data 67Data 35Data 31Data 6Data 16Data 10YData 80Data 49Data 90Data 47Data 64Data 25
3Data 76Data 25Data 81Data 76Data 36Data 90YData 33Data 20Data 64Data 62Data 71Data 46
4
Destroyed
Cell Formulas
RangeFormula
A2:M3A2=FILTER(Master!A2:M1000,Master!G2:G1000="Y","")
Dynamic array formulas.


Phily50.xlsm
ABCDEFGHIJKLM
1Hdr1Hdr2Hdr3Hdr4Hdr5Hdr6DestroyedHdr8Hdr9Hdr10Hdr11Hdr12Hdr13
2Data 13Data 90Data 56Data 39Data 5Data 2NData 57Data 20Data 53Data 36Data 18Data 42
3Data 66Data 21Data 99Data 68Data 47Data 41NData 15Data 99Data 89Data 35Data 3Data 29
4
Retained
Cell Formulas
RangeFormula
A2:M3A2=FILTER(Master!A2:M1000,Master!G2:G1000="N","")
Dynamic array formulas.
 
Upvote 1
This should do the trick.
Code:
Sub Maybe()
Application.ScreenUpdating = False
With Range("A1:M" & Cells(Rows.Count, 2).End(xlUp).Row)
  .AutoFilter 7, "Y"
  .Offset(1).Resize(.Rows.Count - 1).EntireRow.Copy
Sheets("Destroyed").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial
Sheets("Retained").Cells(Rows.Count, 1).End(xlUp).Offset(1).PasteSpecial
  .AutoFilter
End With
Application.ScreenUpdating = True
End Sub

Hi Peter.
"Master - this contains the full data
Destroyed and Retained"
and next
"copies the entire row (columns A to M) into destroyed and retained"

No serious problem but how many Posts have you answered where Sheet names were wrong?
Not much different from where there are leading or trailing spaces.
Might as well get them used to doing it properly.
 
Last edited:
Upvote 0
and next
"copies the entire row (columns A to M) into destroyed and retained"
OK, see what you were getting at now. :)

This should do the trick.
Just wondering if you have actually tested your code though? It didn't work correctly for me.

And also wondering how that code would handle the following without continually adding the same data to the other sheet(s)
I would need this to either update dynamically or through the push of a button prior to exiting.

@Phily50
and then sorting them by date,
I had overlooked this comment previously. If you could advise which column contains the date, my formulas could be adjusted so that the data was also sorted by date (if it isn't already).
 
Upvote 1
All the supplied code does is copy filtered data into the two sheets at the next empty cell in Column A. No sorting. If they want the two sheets cleared before pasting, that would obviously be no problem. But again, to me that falls in the educational part that they should think things thru before asking. And I also realize that most do not think that far ahead but that will have changed for the next time when they will remember to supply all the relevant information hopefully. It will always be an ongoing battle but to me it is the best way of learning.
I am probably wrong but in my opinion, people should put some effort into it themselves by trying to work things out. This has the advantage that they learn something also.
If they cannot get it to work, come back here, explain what you tried and ask for help for that problem.
 
Upvote 0
Yes I did
Fair enough. Wen I ran it this happened:

1. It filtered the data for "Y" (=Destroyed)
2. Copied the filtered data
3. Pasted that data on the Destroyed sheet
4. Pasted exactly the same data (ie the 'destroyed' data) on the Retained sheet (not the 'retained' data)

1675133565961.png
 
Upvote 0
I think I see what you mean. I do have to read Post #1 again a few times to comprehend what is requested and to sort the requirements.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,851
Members
449,051
Latest member
excelquestion515

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