VBA & Macro Problems

Use an Excel VBA Macro to Add Hours to a Matching Week Column

Steve KSteve K Nov 18, 2026 869 views

Question details

Create a button-triggered macro that adds entered working hours to the column for the matching week.

Use an Excel VBA Macro to Add Hours to a Matching Week Column
Product
Microsoft Excel
Device & OS
not provided
Scenario
A user selects entered hours in column B of a project row and clicks an Add button to update that row's weekly total.
Observed behavior
The macro should read the week number from column A, find it in E2:BE2, and increase the existing total in the matching week column.
Before you start

Check that week numbers are in column A, entered hours are in column B, and week headers are in E2:BE2 on the same worksheet. The example assumes project rows start at row 3 and that destination cells contain numbers or are blank, not formulas.

Solution 1Recommended

Assign an Add Hours Macro to an Excel Button

Use this macro when the selected cell contains hours in column B and the week number is immediately to its left.

The macro uses the active worksheet for all cell and range references. If your headers or project rows are in different locations, adjust the ranges and row check in the code.

1
Open the VBA editor

In Excel, open the workbook and select Developer > Visual Basic, then choose Insert > Module. If Developer is hidden, enable it under File > Options > Customize Ribbon.

2
Insert the macro

Paste this code into the module: Sub AddHoursToWeek() Dim ws As Worksheet Dim hoursCell As Range Dim targetCell As Range Dim weekValue As Variant Dim matchPos As Variant Dim currentTotal As Double If TypeName(Selection) <> "Range" Then MsgBox "Select one hours cell in column B." Exit Sub End If If Selection.Cells.CountLarge <> 1 Then MsgBox "Select one hours cell in column B." Exit Sub End If Set ws = ActiveSheet Set hoursCell = Selection If hoursCell.Column <> 2 Or hoursCell.Row < 3 Then MsgBox "Select an hours cell in column B, row 3 or below." Exit Sub End If If IsError(hoursCell.Value2) Then MsgBox "Enter a numeric hours value." Exit Sub End If If IsEmpty(hoursCell.Value2) Or Not IsNumeric(hoursCell.Value2) Then MsgBox "Enter a numeric hours value." Exit Sub End If weekValue = ws.Cells(hoursCell.Row, 1).Value2 If IsError(weekValue) Then MsgBox "Enter a valid week number in column A." Exit Sub End If If IsEmpty(weekValue) Or weekValue = "" Then MsgBox "Enter a week number in column A." Exit Sub End If matchPos = Application.Match(weekValue, ws.Range("E2:BE2"), 0) If IsError(matchPos) Then MsgBox "The week was not found in E2:BE2." Exit Sub End If Set targetCell = ws.Cells(hoursCell.Row, ws.Range("E2").Column + CLng(matchPos) - 1) If targetCell.HasFormula Or IsError(targetCell.Value2) Then MsgBox "The matching total cell contains a formula or error." Exit Sub End If If IsEmpty(targetCell.Value2) Or targetCell.Value2 = "" Then currentTotal = 0 ElseIf IsNumeric(targetCell.Value2) Then currentTotal = CDbl(targetCell.Value2) Else MsgBox "The matching total cell must be blank or numeric." Exit Sub End If targetCell.Value2 = currentTotal + CDbl(hoursCell.Value2) End Sub

3
Add and assign the button

Return to the worksheet and choose Developer > Insert > Button (Form Control). Draw the button, select AddHoursToWeek in the Assign Macro dialog, and click OK. Rename the button Add if desired.

4
Save and test

Choose File > Save As and save the workbook as an Excel Macro-Enabled Workbook (.xlsm). Select an hours cell in column B on the worksheet with the button, click Add, and check the total under the matching week header.

Assign an Add Hours Macro to an Excel Button
Each click adds the hours again: The macro does not clear the entered hours or prevent duplicate submissions. Clicking Add twice for the same selection adds the value twice.
Free Microsoft Office alternative

Manage Weekly Hours in WPS Spreadsheets

WPS Office is a free, lightweight Office alternative with a familiar spreadsheet interface and support for Microsoft Office formats, making it easy to move existing workbooks. Excel VBA compatibility and macro availability vary by WPS version and setup, so test the macro before relying on it.

Organize project hours and weekly totals in spreadsheetsOpen and edit Microsoft Excel XLSX workbooksUse a familiar interface for a smoother transition from ExcelWork with a free, lightweight office suite
microsoft office alternative - wps office

Frequently Asked Questions

Why does the macro say the week was not found?

Check the week number in column A and the headers in E2:BE2 on the active sheet. Make sure the value matches a header exactly; text and numeric week values may need to be made consistent.

Will the macro overwrite an existing weekly total?

It adds the entered hours to a numeric total and treats a blank destination as zero. It stops if the destination contains a formula, an error, or nonnumeric text.

How do I run the macro from a different worksheet?

Activate the worksheet containing the project row, select its hours cell in column B, and use a button on that worksheet. The example reads and writes on the active sheet; a cross-sheet layout requires changing the worksheet references in the VBA editor.

Why can't I run the macro after reopening the workbook?

Save it through File > Save As as an Excel Macro-Enabled Workbook (.xlsm). When reopening it, enable macros only if you trust the workbook and its source.