In this tutorial I am going to explain how to copy records to another sheet based on a condition. I explore three methods:
- Filter and then copy/paste – this method is easiest for beginners
- Use the FILTER function – this method won’t work in Excel versions prior to Excel 365
- Use a VBA macro – this method is the most automated
5 FREE EXCEL TEMPLATES
Plus Get 30% off any Purchase in the Simple Sheets Catalogue!
Click here to download the featured file (including code).
METHOD 1: Filter and then copy/paste
Method 1 used the filtering capabilities in Excel. To use this method:
- Select a cell within your data
- Use the shortcut CTRL SHIFT L to display the filter buttons
- Filter out any records that you don’t want to copy
- Select a cell within your data
- Use the shortcut CTRL A to select all the data
- Use the shortcut CTRL C to copy the data (this will copy visible records only)
- Navigate to the worksheet that you want to copy the records to
- Click in the cell that you want to paste your records into
- Use the shortcut CTRL V or ENTER to paste the copied records
METHOD 2: Use the FILTER function
Method 2 uses the FILTER function. The FILTER function is not available in versions of Excel prior to Excel 365. To use this method:
- In a new worksheet enter column headings (the same as the column headings in your data)
- In the cell directly underneath the first column heading, enter a formula using the FILTER function.
The FILTER function has three arguments
array is the array of values you want to filter (your original data set)
include is a logical test that specifies what records to include
[if_empty] allows you to specify what to return if no records meet your test – this is non-mandatory
Example FILTER formula featured in the video: = FILTER(Data!A2:E14,Data!E2:E14=”Over budget”)
METHOD 3: Use a VBA Macro
Method 3 uses the VBA macro below:
Sub CopyOverBudgetRecords() Dim StatusCol As Range Dim Status As Range Dim PasteCell As Range 'You will need to adapt this code for your scenario - follow the STEPS below 'STEP1: Change the sheet name and range in the line of code below to match the sheet name 'and range holding the data that you want to copy rows from. Set StatusCol = Sheet1.Range("E2:E14") For Each Status In StatusCol 'STEP2: Change the sheet name and range in the lines of code below to match the sheet name 'and cell that you want to copy your data to. You only need to specify one cell - 'the first cell you will copy to. If Sheet5.Range("A2") = "" Then Set PasteCell = Sheet5.Range("A2") Else 'STEP3: In the line of code below, the range should refer to your first column heading Set PasteCell = Sheet5.Range("A1").End(xlDown).Offset(1, 0) End If 'STEP4: I have included three ways of copying the data. To use one of the methods, delete 'the apostrophe before the words IF Status at the beginning of the line. 'You can only use one of the options. The third option is currently active. 'This one was used in the video, but will only work if your criteria is in column 5 'and you have five cells per record 'If Status = "Over budget" Then Status.Offset(0, -4).Resize(1, 5).Copy PasteCell 'This one copies the entire row - right across the worksheet 'If Status = "Over budget" Then Status.EntireRow.Copy PasteCell 'This one only copies the relevant cells, rather than the entire row and it doesn't 'matter which row contains the criteria or how many cells you need to copy. 'It won't work, however, if you have blank cells in your data. If Status = "Over budget" Then Range(Status.End(xlToLeft), Status.End(xlToRight)).Copy PasteCell Next Status End Sub
To use this VBA macro:
- Open the Visual Basic Editor (VBE) using the shortcut ALT F11
- Click Insert | Module to create a module within the VBA project
- Paste the code I have provided into the code window of the new module
- Make changes to the code as explained in the video https://youtu.be/W4swMZe0TEE?t=237
or follow the instructions included within the code - Use the shortcut F5 to run the macro
- Use the shortcut ALT Q to close the VBE
- When you use VBA, you must save your workbook as Macro-Enabled