VBA & Macro Problems

How to Find and Insert Missing Hourly Records in Excel

Rana GarciaRana Garcia Nov 4, 2026 868 views

Question details

Identify missing hourly timestamps in a year-long dataset and accurately insert blank rows or filled time values to correct the chronological sequence.

How to Find and Insert Missing Hourly Records in Excel
Product
Excel
Device & OS
not provided
Scenario
Managing a full year's continuous hourly dataset (which should contain 8,760 values) where some hourly timestamps are missing due to system recording failures.
Observed behavior
The dataset skips missing hours entirely without leaving blank spaces, currently forcing the user to find gaps and add missing rows manually.
Before you start

Always create a backup copy of your workbook before running VBA macros that insert or delete rows, as macro actions typically cannot be undone using the standard Undo feature.

Solution 1Recommended

Use a VBA Macro to Automatically Insert Missing Hours

A VBA macro is the most efficient way to loop through thousands of rows, detect time gaps greater than one hour, and automatically insert the missing timestamps.

By comparing adjacent timestamps from the bottom of your dataset to the top, a VBA macro can identify gaps efficiently. Once a gap exceeding one hour is found, the script inserts a new row and populates it using the DateAdd function.

1
Open the VBA Editor

Press 'Alt + F11' in Excel to open the Visual Basic for Applications (VBA) editor, then click 'Insert' > 'Module' from the top menu to create a new blank script window.

2
Write the Loop and Logic

Write a script that loops backwards (e.g., For i = LastRow To 2 Step -1). Inside the loop, use the 'DateDiff' function in VBA to compare the timestamp in cell(i) with cell(i-1).

3
Insert Rows and Populate Time

If the difference is greater than 60 minutes, use 'Rows(i).EntireRow.Insert' to add a new row. Use the 'DateAdd' function to add exactly one hour to the preceding timestamp and place it in the newly created blank cell.

4
Run the Macro

Close the VBA editor and return to your worksheet. Go to the 'Developer' tab, click 'Macros', select your newly created macro, and click 'Run'.

Efficient Data Processing

Manage Large Datasets and Macros Easily with WPS Spreadsheet

WPS Office offers a highly capable Spreadsheet application that fully supports advanced formulas and VBA macros, making it incredibly simple to identify gaps and fill missing hourly records in massive yearly datasets.

  1. 1. Download and Open: Install WPS Office for free and open your hourly dataset file (.xlsx or .csv) in WPS Spreadsheet.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon to easily access the VBA Editor and manage your macros.
  3. 3. Automate the Process: Input your time gap macro or helper formulas directly, executing the gap-filling process seamlessly just like you would in Excel.
Full compatibility with Microsoft Excel (.xlsx and .xlsm formats)Built-in VBA editor for automating complex data entry tasksHandles large 8,760-row datasets smoothly without laggingCompletely free and lightweight suite for everyday office work
QA img-9

Frequently Asked Questions

How many hourly records should a standard one-year dataset contain?

A standard non-leap year contains exactly 8,760 hourly records (365 days multiplied by 24 hours). If it is a leap year, the dataset will contain 8,784 records.

Why does my time calculation formula return a #VALUE! error?

This error occurs if your timestamp column is formatted as text instead of Date/Time. The software requires numeric date-time values to perform mathematical operations. Select the column, go to 'Format Cells', and change the type to a Custom Date/Time format.

Can I automatically estimate the data values for the missing hours?

Yes. Once the blank rows are inserted, you can use linear interpolation formulas to estimate the missing data points by calculating the average between the preceding and succeeding known values. VBA can also be programmed to do this automatically during row insertion.