VBA & Macro Problems

Calculate Hours Worked by Row in an Excel VBA UserForm

Elise WilliamsElise Williams Nov 22, 2026 869 views

Question details

Calculate hours worked for the correct UserForm row whenever its start or finish time changes.

Calculate Hours Worked by Row in an Excel VBA UserForm
Product
Microsoft Excel VBA UserForm
Device & OS
not provided
Scenario
A UserForm contains multiple rows of start, finish, and hours-worked controls with matching row identifiers.
Observed behavior
Each row needs to update its own hours-worked control instead of always calculating the same row.
Before you start

Check that each row uses matching control names, such as L_WL_Start_R05, L_WL_Finish_R05, and L_WL_HoursWorked_R05. Open the workbook in desktop Excel, and enable macros only if you trust the file.

Solution 1Recommended

Use a shared VBA procedure to calculate each UserForm row

Pass the changed control's row identifier to one procedure that finds the matching controls and calculates elapsed hours.

This example assumes the controls are directly on the UserForm and that the hours-worked control accepts a Value. It calculates same-day shifts; overnight shifts need an additional adjustment.

1
Open the UserForm code

In desktop Excel, select Developer > Visual Basic. In the VBA editor, find your UserForm in Project Explorer and open its code window.

2
Add the shared calculation

Paste this procedure into the UserForm code module: Private Sub CalculateHours(ByVal rowId As String) Dim startText As String Dim finishText As String Dim startTime As Date Dim finishTime As Date startText = Trim$(Me.Controls("L_WL_Start_" & rowId).Value & "") finishText = Trim$(Me.Controls("L_WL_Finish_" & rowId).Value & "") If Not IsDate(startText) Or Not IsDate(finishText) Then Me.Controls("L_WL_HoursWorked_" & rowId).Value = "" Exit Sub End If startTime = TimeValue(startText) finishTime = TimeValue(finishText) Me.Controls("L_WL_HoursWorked_" & rowId).Value = _ Format$((finishTime - startTime) * 24, "0.00") End Sub

3
Connect both row events

In the same UserForm code module, add these events for row R05: Private Sub L_WL_Start_R05_Change() CalculateHours "R05" End Sub Private Sub L_WL_Finish_R05_Change() CalculateHours "R05" End Sub Repeat the two event procedures for each row, replacing R05 with the corresponding identifier in both the event name and the procedure call.

4
Test and save

Run the UserForm and select valid start and finish times in row R05. Confirm that L_WL_HoursWorked_R05 updates, then test another row and save the workbook as an Excel Macro-Enabled Workbook (.xlsm).

Use a shared VBA procedure to calculate each UserForm row
Overnight shifts and control containers: For an overnight shift, add 1 to finishTime when it is earlier than startTime before calculating the difference. If controls are inside a Frame or another container, reference that container's Controls collection instead of Me.Controls.
Free Microsoft Office alternative

Track Work Hours in WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for editing timesheets and moving between spreadsheet formats. It does not fix Excel VBA UserForms; check macro compatibility before migrating a workbook that relies on them.

Create and edit work-hour timesheets in WPS SpreadsheetOpen and save common Microsoft Excel formats, including XLSXUse a familiar spreadsheet interface for an easier transitionWork with a free, lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA code show an invalid qualifier error?

A String variable does not have a .Value property. Read .Value from the UserForm control, then use TimeValue to convert the text before subtracting the times.

Why do I need to multiply the time difference by 24?

Excel and VBA represent a day as 1, so subtracting two times returns a fraction of a day. Use (finishTime - startTime) * 24 to convert that difference to hours.

How can I calculate an overnight shift?

After converting the values with TimeValue, add 1 to finishTime if it is earlier than startTime. Then calculate (finishTime - startTime) * 24. This assumes the finish time is on the following day.

What if a time control is empty or contains invalid text?

The example checks each entry with IsDate. If either value is invalid, it clears the matching hours-worked control and exits until valid times are entered.

Why can't Me.Controls find a control inside a Frame?

Me.Controls looks in the UserForm's own control collection. If the controls are inside a Frame, reference the Frame's Controls collection and verify that the constructed names match the controls' Name properties.