VBA & Macro Problems

How to Insert Blank Rows When the Date Changes in Excel

Emma BrownEmma Brown Nov 10, 2026 870 views

Question details

The user needs to insert a blank row between different date groups in a large dataset after sorting the data.

How to Insert Blank Rows When Date Changes in Excel
Product
Excel
Device & OS
not provided
Scenario
Organizing mixed stock and date data to separate records visually by date groups for easier reading and printing.
Observed behavior
The dataset currently lacks visual separation between different date groupings, and manual insertion is too time-consuming for large datasets.
Before you start

Ensure your dataset is properly sorted by the date column (Column A) from oldest to newest before running any macros. It is also highly recommended to save a backup copy of your workbook to prevent accidental data loss.

Solution 1Recommended

Insert Blank Rows Using a VBA Macro (Reverse Loop)

The most efficient way to insert rows conditionally in large datasets without disrupting row indices is to use a reverse-loop VBA macro.

When inserting or deleting rows in Excel using VBA, looping from the bottom up (a reverse loop) is crucial. If you loop top-down, adding a row shifts everything down, causing the macro to evaluate the same row repeatedly or skip rows altogether.

The following script checks Column A for date changes and inserts a blank row whenever a difference is detected between adjacent cells.

1
Open the VBA Editor

Press `Alt + F11` on your keyboard to open the Microsoft Visual Basic for Applications Editor in Excel.

2
Insert a New Module

Click on `Insert` from the top menu bar, and then select `Module` to create a new blank module for your code.

3
Paste the VBA Code

Copy and paste the following code into the module window: Sub Adjust() Dim i As Long, j As Long i = Cells(Rows.Count, 1).End(xlUp).Row For j = i To 2 Step -1 If Cells(j, 1).Value <> Cells(j - 1, 1).Value Then Rows(j).Insert End If Next j End Sub

4
Run the Macro

Close the VBA Editor and return to your worksheet. Press `Alt + F8` to open the Macro dialog box, select `Adjust` from the list, and click `Run`.

Insert Blank Rows Using a VBA Macro (Reverse Loop)
Sort First: Make sure your data is sorted by the dates in Column A before running this macro; otherwise, it will insert rows at every random date change, creating a messy worksheet.
Advanced Spreadsheet Capabilities

Organize Your Datasets Easily with WPS Spreadsheet

WPS Office offers a fully-featured Spreadsheet program that supports advanced VBA macros and seamless data manipulation. You can easily sort data and run automated scripts to insert blank rows, making data grouping effortless and highly efficient.

  1. 1. Open Your File in WPS: Launch WPS Spreadsheet and open your existing Excel dataset containing the date column.
  2. 2. Sort by Date: Select your data, navigate to the 'Data' tab, and click 'Sort' to arrange your records chronologically by Column A.
  3. 3. Enable Macros: Navigate to the 'Developer' tab, open the VBA Editor, and paste the reverse-loop macro code to process your data.
  4. 4. Run the Macro: Execute the macro to instantly insert blank rows wherever the date changes, cleanly separating your date groups.
Fully compatible with Microsoft Excel (.xlsx) formatsBuilt-in support for VBA and macros to automate repetitive tasksLightweight, fast, and completely free to download and useFamiliar user interface requiring zero learning curve
microsoft office alternative - wps office

Frequently Asked Questions

Can I use this macro for columns other than Column A?

Yes. You can change `Cells(j, 1)` and `Cells(j - 1, 1)` in the VBA code to match the column number you are checking. For example, replace the `1` with `2` if your dates are in Column B.

Why did the macro insert rows at almost every single line?

This usually happens if your data was not sorted by the date column before running the macro. You must sort the column chronologically so identical dates are grouped together; otherwise, the macro sees a 'change' on every line.

How do I undo the macro if it inserts rows incorrectly?

VBA macro actions generally clear the undo history and cannot be undone using the standard `Ctrl + Z` shortcut. It is highly recommended to save a backup of your workbook before running any macro.

Can I insert multiple blank rows instead of just one?

Yes, you can easily modify the code. Change the line `Rows(j).Insert` to `Rows(j).Resize(2).Insert` to insert two blank rows instead of one. Adjust the number in parentheses as needed.