VBA & Macro Problems

How to Calculate Excel Work Hours Only During Business Hours

Amos GikundaAmos Gikunda Nov 3, 2026 869 views

Question details

The user wants to calculate the elapsed work time strictly between 9:00 AM and 5:00 PM on workdays, accounting for periods when the workbook is closed.

How to Calculate Elapsed Work Hours in Excel During Business Hours
Product
Excel
Device & OS
not provided
Scenario
Tracking continuous work hours or elapsed process time across multiple sessions while ignoring non-business hours, weekends, and holidays.
Observed behavior
VBA functions like Application.OnTime only run when Excel is actively open, making it impossible to actively track time in the background when the workbook is closed.
Before you start

Create a separate worksheet named 'Holidays' containing a list of your company's official holiday dates so they can be accurately excluded from your elapsed business hours calculations.

Solution 1Recommended

Log Open/Close Timestamps with VBA and Calculate Retrospectively

Use Workbook event handlers to automatically log the exact times the file is opened and closed, then use formulas to calculate the elapsed business hours.

VBA code cannot execute while the Excel application is closed, which means functions like Application.OnTime cannot track time in the background. The most reliable workaround is to capture the exact timestamps when the workbook is opened and closed.

By saving these timestamps in a hidden sheet, you can later use date and time formulas to compute the hours that fall strictly within your 9:00 AM to 5:00 PM schedule, naturally excluding time when the file was closed or during the weekend.

1
Set up a hidden tracking sheet

Create a new worksheet in your workbook and name it 'TimeLog'. This sheet will act as a database to store the opening and closing timestamps.

2
Add the Workbook_Open event

Press ALT + F11 to open the VBA Editor. Double-click 'ThisWorkbook' in the Project Explorer. Create a Workbook_Open subroutine that finds the next empty row on the 'TimeLog' sheet and inputs the current timestamp using the Now() function.

3
Add the Workbook_BeforeClose event

In the same 'ThisWorkbook' module, create a Workbook_BeforeClose subroutine. This code should log the closing timestamp in the adjacent column of your 'TimeLog' sheet. Ensure you include a ThisWorkbook.Save command so the recorded times are not lost.

4
Calculate business hours

In a separate reporting sheet, use a combination of the NETWORKDAYS function and standard time formulas referencing the 'TimeLog' sheet. Calculate the difference between the open and close timestamps, restricting the valid time to > 9:00 AM and < 5:00 PM.

Log Open/Close Timestamps with VBA and Calculate Retrospectively
Auto-saving requirement: Because the BeforeClose event writes data to the workbook, forcing an automatic save (ThisWorkbook.Save) is necessary. If auto-saving is not acceptable for your workflow, you may need to prompt the user to save changes manually.

Track Business Hours Seamlessly with WPS Office

WPS Spreadsheet provides a robust environment for managing complex date and time calculations. With full support for VBA macros and advanced formulas, you can easily build automated time-tracking logs to measure elapsed business hours.

  1. 1. Open your workbook: Launch WPS Spreadsheet and open your existing time-tracking file.
  2. 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click 'VBA Editor' to insert your timestamp logging scripts.
  3. 3. Apply time calculations: Use built-in functions like NETWORKDAYS.INTL directly in your spreadsheet to exclude weekends and specified holidays from your time logs.
  4. 4. Save with Macros: Go to Menu > Save As, and choose 'Excel Macro-Enabled Workbook (*.xlsm)' to ensure your tracking scripts are preserved.
Fully compatible with Microsoft Excel file formats, including Macro-Enabled Workbooks (.xlsm).Robust VBA Editor support to effortlessly implement Workbook_Open and BeforeClose event logging.Built-in date and time functions, including NETWORKDAYS, for accurate business hour calculations.Lightweight architecture ensures your files load fast, even with complex tracking scripts.
microsoft office alternative - wps office

Frequently Asked Questions

Why doesn't Application.OnTime work when Excel is closed?

Application.OnTime is a macro method that relies on the Excel application engine running in the background. When the workbook or the Excel application is entirely closed, VBA execution stops completely, preventing the timer from running.

Can I exclude custom company holidays from the business hours calculation?

Yes. You can use the NETWORKDAYS or NETWORKDAYS.INTL function in your formula and reference a specific range of cells containing your custom holiday dates. The function will automatically subtract those dates from the total working days.

How do I hide the timestamp logging sheet from users?

You can right-click the 'TimeLog' sheet tab and select 'Hide'. For more security, open the VBA Editor, select the sheet in the Project window, go to the Properties pane, and change the 'Visible' property to '2 - xlSheetVeryHidden'. This prevents users from unhiding it via the standard Excel interface.