VBA & Macro Problems

How to Reset and Increment Page Numbers by Date in Excel VBA

John WilsonJohn Wilson Oct 23, 2026 868 views

Question details

The user wants to use an Excel VBA macro to reset the page number in cell L1 to 1 between December 31 and January 2, while incrementing it by 1 on all other dates.

How to Reset and Increment Page Numbers by Date in Excel VBA
Product
Excel
Device & OS
not provided
Scenario
Automating page numbers or counters based on specific calendar events, specifically resetting at the transition of the new year.
Observed behavior
The cell value needs a dynamic update mechanism where it automatically resets to 1 during the specified three-day window and adds 1 to the existing value otherwise.
Before you start

Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools in your ribbon to access the VBA editor.

Solution 1Recommended

Use VBA DateSerial to Conditionally Update the Cell

Write a VBA macro that uses the DateSerial function to identify the year-end date range and updates cell L1 based on whether the current date falls within that window.

The DateSerial function in VBA creates a date based on specific year, month, and day inputs. By dynamically pulling the current year, you can compare the system's current date to December 31 of the previous year and January 2 of the current year.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications Editor. In the Project Explorer pane on the left, right-click your workbook, select Insert, and choose Module.

2
Define the Year-End Date Boundaries

Create a new Sub procedure. Declare variables for your start and end dates. Use DateSerial(Year(Date), 1, 2) to get January 2nd, and DateSerial(Year(Date) - 1, 12, 31) for December 31st if evaluating from the new year.

3
Write the Conditional Logic

Use an If statement to check if the current Date falls between your defined start and end dates. Since December 31st and January 1st cross the year boundary, ensure your logic accounts for the current year dynamically.

4
Assign the Reset or Increment Values

Inside the If block covering Dec 31 to Jan 2, add the line Range("L1").Value = 1. In the Else block, check if Range("L1").Value <= 0. If it is, set it to 1; otherwise, use Range("L1").Value = Range("L1").Value + 1 to increment it.

5
Run the Macro

Close the VBA editor and return to Excel. Press Alt + F8 to open the Macro dialog, select your newly created script, and click Run to update cell L1 based on today's date.

Use VBA DateSerial to Conditionally Update the Cell
Handling Nonpositive Values: If the cell is empty or contains text when the macro runs outside the reset dates, it may cause an error or unwanted result. Adding a check to set nonpositive or empty values to 1 ensures the increment works flawlessly.
Advanced Macro Capabilities

Automate Workflows Flawlessly with WPS Spreadsheet

WPS Spreadsheet offers robust, built-in support for VBA macros. You can run conditional formatting, advanced date checks, and automatic cell operations using the exact same code without modifying your existing Excel scripts.

  1. 1. Open Your Workbook in WPS: Launch WPS Spreadsheet and open your target workbook.
  2. 2. Access the Macro Editor: Navigate to the Developer tab on the top ribbon and click on 'Visual Basic' or 'Macros'.
  3. 3. Insert Your VBA Script: Paste your DateSerial conditional script into a new Module to handle the page number updates.
  4. 4. Execute the Code: Run the macro directly within WPS Spreadsheet to instantly reset or increment the value in cell L1.
Fully compatible with Microsoft Excel Macro-Enabled (.xlsm) files and standard VBA scripts.Lightweight architecture ensures faster load times and smoother macro execution.Comprehensive Developer tab makes inserting modules and managing code seamless.Free to use for everyday office tasks with an intuitive, tabbed interface.
microsoft office alternative - wps office

Frequently Asked Questions

How do I make this macro run automatically when I open the workbook?

You can trigger the macro upon opening the file by placing your code inside the Workbook_Open event. In the VBA Editor, double-click 'ThisWorkbook' in the Project Explorer, choose 'Workbook' from the left dropdown above the code window, and call your macro within the Private Sub Workbook_Open() block.

What exactly does the DateSerial function do in Excel VBA?

The DateSerial function returns a date variable representing a specific year, month, and day. It is highly useful for dynamic date calculations because it automatically handles leap years and month rollovers. For example, DateSerial(2023, 1, 0) correctly returns December 31, 2022.

Can I target a different cell or multiple sheets with this macro?

Yes. To target a different cell, simply change Range("L1") in your VBA code to your desired cell, such as Range("A1"). To target a specific sheet, qualify the range with the sheet name, for example, Worksheets("Sheet1").Range("L1").Value.