I have a workbook in which staff members input their days worked on one tab. On a separate tab, they choose expense categories from drop-down menus that automatically calculate expenses.
It works fine if they input their hours first and then pick the expense category. However, if they then go back and change their hours worked, the expenses on the other tab do not recalculate unless they reselect the expenses.
Is there any way to add a line to the code so it recalculates when changes are made on the hours worked tab?
Attached is an example workbook. You can see how initially the 10 days worked for employee A results in $390 printing expenses on the other tab. But, if you then change it to 5 days worked, the printing expenses remain $390 unless you relesect "Printing" from the drop-down menu.
Below is the code on the expense page.
Thanks in advance.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim a As Double, b As Integer, c As Range, d As Integer, e As Integer
ActiveSheet.Unprotect
Application.EnableEvents = False
' The Target is the particular cell result you are checking and unless the active cell is
' one of those defined the macro does not execute
If Not Intersect(Target, Range("e5,i5,l5,e28,i28")) Is Nothing Then
' This part clears complete range before writing new, or when the target cell is cleared.
Range(Target.Offset(1, 0), Target.Offset(16, 1)).ClearContents
Range(Target.Offset(1, 0), Target.Offset(16, 1)).Locked = False
' Here the Match function is used to lookup the values associated with the drop down list
' it checks the range for applicable values and copies them to the target range.
If Target.Value <> "" Then
Let a = Application.Match(Target.Value, Range("Source"), 0)
For e = 23 To 52 Step 3
If Cells(a, e).Value <> "" Then Let b = b + 1 ' allows for up to 10 items
Next e
If b > 0 Then
For d = 1 To b
Let Target.Offset(d, 0).Value = Cells(a, 20 + d * 3).Value
Let Target.Offset(d, 0).Locked = True
Let Target.Offset(d, 1).Value = Cells(a, 20 + d * 3 + 1).Value
If Cells(a, 20 + d * 3 + 2).Value = "x" Then
Let Target.Offset(d, 1).Locked = False
Else
Let Target.Offset(d, 1).Locked = True
End If
Next d
End If
End If
End If
ActiveSheet.Protect
Application.EnableEvents = True
End Sub
Bookmarks