Match Data in Two Excel Columns and Align Missing Values via VBA
Question details
The user needs to align source data in columns B through D with a master list of numbers in column A by inserting blank cells where source numbers are missing.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Comparing a master numerical list against a source dataset with gaps in a large workbook containing over 40,000 rows.
- Observed behavior
- The source data in columns B through D does not align with the master numbers in column A due to missing values in the source dataset.
Before running any VBA macro that modifies the layout of your dataset, ensure you have saved a backup copy of your workbook, as macro actions cannot be undone using the standard Undo shortcut.
Use a VBA Macro to Compare Columns and Insert Blank Cells
A custom VBA macro is the most efficient way to process large datasets by automatically comparing columns and inserting blank cells downward where source numbers are missing.
This macro will loop through your master list in column A and compare it to the source data in column B. If a mismatch is detected, indicating a missing value, it shifts the corresponding cells in columns B through D down by inserting blank cells.
To optimize performance for large workbooks with tens of thousands of rows, the macro temporarily disables screen updating during its execution.
Press Alt + F11 on your keyboard to launch the Microsoft Visual Basic for Applications window.
In the top menu bar, click on 'Insert' and select 'Module' to create a blank script window.
Write or paste the VBA code designed to loop through column A, compare it with column B, and use the 'Range.Insert Shift:=xlDown' command for columns B:D when a mismatch is found.
Ensure your code includes 'Application.ScreenUpdating = False' at the very beginning and 'Application.ScreenUpdating = True' at the end to prevent the screen from flashing and to speed up execution.
Press F5 or click the green 'Run' triangle button in the toolbar to execute the macro and align your dataset.
Run VBA Macros to Align Data with WPS Spreadsheet
WPS Spreadsheet offers robust support for VBA macros, allowing you to seamlessly process large datasets, compare columns, and automatically align missing values exactly as you would in Microsoft Excel.
- 1. Open Your Dataset: Launch WPS Spreadsheet and open the workbook containing the unaligned columns.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the main ribbon and click on 'VBA Editor' to open the coding environment.
- 3. Insert and Run the Macro: Click 'Insert' > 'Module', paste your column alignment macro, and press 'F5' to instantly align your missing values.

Frequently Asked Questions
Why does my macro take so long to run on 40,000 rows?
Processing tens of thousands of rows cell-by-cell is resource-intensive. To drastically speed up the macro, ensure you add 'Application.ScreenUpdating = False' and 'Application.Calculation = xlCalculationManual' at the beginning of your script, and restore them to 'True' and 'xlCalculationAutomatic' at the end.
Can I align data in two columns using formulas instead of VBA?
Yes, you can use lookup formulas like XLOOKUP, VLOOKUP, or INDEX/MATCH in empty helper columns to pull the corresponding data from the source columns based on the master numbers in column A. However, VBA is much faster if you want to physically shift and insert blank cells in the existing columns without creating new ones.
How do I enable the Developer tab to access the VBA Editor?
Go to 'File' > 'Options' > 'Customize Ribbon'. In the list of Main Tabs on the right side of the dialog box, check the box next to 'Developer' and click 'OK'. This will add the Developer tab to your ribbon, giving you direct access to the VBA Editor and Macro security settings.




