How to Use a VBA Macro to Copy Matching Values to Another Worksheet in Excel
Question details
The user needs a VBA macro to search for a specific value ('Y') in a column (Column C) and copy the corresponding data from another column (Column F) to the next available empty row on a different worksheet.

- Product
- Spreadsheet
- Device & OS
- not provided
- Scenario
- Automating data extraction and transfer between worksheets based on a specific condition.
- Observed behavior
- Needs to identify matching rows and dynamically place the target values into the next available row on a destination sheet without triggering paste-area size mismatches.
Ensure you have saved your workbook as a Macro-Enabled Workbook (.xlsm) and that the Developer tab is enabled to access the VBA editor.
Create a VBA Macro to Copy Specific Column Values Based on Criteria
Use this method to loop through your data, check for the condition ('Y' in Column C), and copy only the specific corresponding value (Column F) to the next empty row.
This approach uses variables to track both the source sheet and destination sheet. It dynamically calculates the last used row on the destination sheet to ensure data is appended without overwriting existing records.
Press 'Alt + F11' to open the Visual Basic for Applications (VBA) editor in your spreadsheet.
Click 'Insert' from the top menu and select 'Module' to create a blank script window for your code.
Write a script that loops through the source sheet. Use an 'If' statement to check if cells in Column C equal 'Y'. If true, copy the offset cell in Column F to the destination sheet using 'Cells(Rows.Count, 1).End(xlUp).Row + 1' to find the next empty row.
Press 'F5' or click the 'Run' button. The matching values from Column F will be successfully copied to Column A of your destination worksheet.

Modify the Macro to Copy the Entire Row Instead of a Single Value
If you need all the data from the matching row rather than just Column F, adjust the macro to copy and paste the entire row.
Use WPS Spreadsheet to Easily Run VBA Macros for Data Filtering
WPS Office offers robust support for VBA macros, allowing you to seamlessly run your scripts to copy and paste matching values across worksheets. It provides an intuitive interface and reliable compatibility with standard Excel VBA syntax.
- 1. Download and Install: Download WPS Office and launch the WPS Spreadsheet application.
- 2. Open your Macro Workbook: Open your .xlsm file containing the source data and VBA scripts.
- 3. Enable the Developer Tab: Navigate to the settings to display the Developer tab, then click 'Macros' to manage your scripts.
- 4. Run the Script: Select your macro and click 'Run' to instantly filter and copy your matching rows or values.

Frequently Asked Questions
Why do I get a paste-area size mismatch error when copying entire rows?
This error occurs if you try to paste an entire row starting in any column other than Column A (e.g., Column B). An entire row contains the maximum number of columns allowed in a sheet, so shifting the paste area to the right exceeds the spreadsheet's maximum column capacity.
How do I find the next available empty row using VBA?
You can find the next empty row dynamically in VBA using a code snippet like `NextRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1`. This looks up from the very bottom of Column A to find the last used row and adds 1.
Can I copy values based on multiple conditions instead of just one?
Yes, you can modify the `If` statement in your VBA loop to include multiple criteria using logical operators like `And` or `Or` (for example, `If cell.Value = "Y" And cell.Offset(0, 1).Value = "Yes" Then`).




