VBA & Macro Problems

How to Insert Rows in Excel VBA When Column J Changes

Steve KSteve K Oct 23, 2026 873 views

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.

How to Insert Rows in Excel VBA When Column J Changes
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

In Excel, press ALT + F11 on your keyboard to open the Visual Basic for Applications Editor.

2
Insert a New Module

Click 'Insert' in the top menu and select 'Module' to create a blank code window for your script.

3
Create the Bottom-Up Loop

Write a Sub procedure that determines the last row containing data, then loops backwards. For example: For i = LastRow To 3 Step -1.

4
Check for Value Changes

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.

5
Insert the New Row Cells

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.

6
Preserve Columns C and D

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.

Use a Bottom-Up VBA Loop to Insert and Shift Cells
Test on a Copy: VBA actions cannot be reversed using the standard Undo (Ctrl+Z) feature. Always test macros on a copy of your workbook first to avoid accidental data loss.
Advanced VBA Support

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. 1. Open your workbook in WPS: Launch WPS Spreadsheets and open your .xlsm or .xlsx file containing the tracking dataset.
  2. 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. 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.
Fully compatible with Microsoft Excel VBA code and .xlsm formatsBuilt-in Macro Editor for creating and debugging scripts using identical shortcuts (Alt + F11)Lightweight application that executes complex loops and macros quicklyFree to download and use as a complete Microsoft Office alternative
microsoft office alternative - wps office

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.