How to Insert Rows in Excel VBA When Column J Changes
Question details
The user needs a VBA macro to insert a blank row whenever the vehicle tracking ID in column J changes, without shifting the data in columns C and D.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Organizing vehicle tracking data where visual separation is needed between different tracking IDs in a large dataset.
- Observed behavior
- The goal is to automatically detect value changes in column J, insert a new row spanning columns A through J, and keep the existing data in columns C and D in their original positions.
Ensure your tracking data is sorted by column J (vehicle tracking ID) before running the macro, as the script compares adjacent cells to detect changes.
Use a Bottom-Up VBA Loop to Insert and Shift Cells
Using a bottom-up loop ensures that inserting new rows doesn't offset your loop counter. This method checks column J for changes, inserts a partial row, and deletes the shifted cells in columns C and D to restore their alignment.
When modifying row structures in Excel using VBA, always loop backwards (from the bottom up). If you loop top-down, inserting a row pushes the remaining data down, causing the loop to skip rows or process the same row twice.
By inserting cells only across columns A through J instead of the entire row, and then shifting columns C and D back up, you can perfectly preserve the positions of those specific columns.
In Excel, press ALT + F11 on your keyboard to open the Visual Basic for Applications Editor.
Click 'Insert' in the top menu and select 'Module' to create a blank code window for your script.
Write a Sub procedure that determines the last row containing data, then loops backwards. For example: For i = LastRow To 3 Step -1.
Inside the loop, use an If statement to compare the current cell in column J with the one above it: If Cells(i, 10).Value <> Cells(i - 1, 10).Value Then.
If the values differ, insert a blank space from column A to J and push the data down: Range(Cells(i, 1), Cells(i, 10)).Insert Shift:=xlDown.
Immediately after inserting, delete the newly created blank cells in columns C and D to pull that specific data back up: Range(Cells(i, 3), Cells(i, 4)).Delete Shift:=xlUp.

Run Excel Macros Seamlessly in WPS Office
WPS Spreadsheets provides robust support for Excel VBA macros. You can seamlessly run, edit, and create VBA scripts directly within the WPS Macro Editor to automate complex data organization tasks.
- 1. Open your workbook in WPS: Launch WPS Spreadsheets and open your .xlsm or .xlsx file containing the tracking dataset.
- 2. Access the Macro Editor: Go to the 'Tools' tab and click on 'Macro', or press ALT + F11 to open the built-in VBA Editor.
- 3. Run your Script: Insert a new Module, paste your bottom-up loop VBA code, and click 'Run' to automatically format your rows and columns.

Frequently Asked Questions
Why do I need to use a bottom-up loop instead of a standard top-down loop for inserting rows?
When you insert a row using a top-down loop, the row numbers of all data below it shift down. This causes the loop to evaluate the same row again or skip unprocessed rows. A bottom-up loop avoids this by working upwards, ensuring that shifted rows are already processed and do not affect the loop counter.
How do I save an Excel workbook that contains a VBA macro?
You must save the file as an Excel Macro-Enabled Workbook (.xlsm). If you save it as a standard Excel Workbook (.xlsx), Excel will permanently strip and delete all the VBA code from the file.
Can I modify this macro to trigger on a different column instead of column J?
Yes. You can change the column index in the Cells(row, column) reference within your VBA code. Column J is the 10th column, which is why it uses Cells(i, 10). To check column H instead, you would change that number to 8.




