I have created a sub that checks if changes has happened in column F and writes the timestamp to a corresponding cell in column G. How do i edit this sub to return the network days in column H by finding the difference between the timestamp in column H and Cell containing a week commencing date A1? Without VBA, the formula is =ABS(NETWORKDAYS(A1, B1) - SIGN(NETWORKDAYS(A1, H1)). Below is my code so far. Any help?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Application.EnableEvents = False
With Target
'check if change happened in column F
If .Column = 6 Then
'check if changed value is X
If Not IsEmpty(c) Then
'add datestamp if it is
Cells(.Row, 7).Value = Format(DateTime.Now, "yyyy-MM-dd hh:mm:ss")
Cells(.Row, 8).Value = ABS(NETWORKDAYS(G5,H2)-SIGN(NETWORKDAYS(G5,H2)
Else
'clear datestamp and Column H if not
Cells(.Row, 7).Value = ""
Cells(.Row, 8).Value = ""
End If
End If
End With
Application.EnableEvents = True
End Sub
'
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Dim d1 As Date, d2 As Date, wf As WorksheetFunction
Dim N As Long
Set wf = Application.WorksheetFunction
Application.EnableEvents = False
With Target
'check if change happened in column F
If .Column = 2 Then
'check if changed value is X
If Not IsEmpty(c) Then
'add datestamp if it is
d1 = Cells.Range("A1")
d2 = Cells.Range("B1:B2")
N = wf.NetworkDays(d1, d2)
Cells(.Row, 4).Value = N
Else
'clear datestamp and Colunm H if not
Cells(.Row, 4).Value = ""
End If
End If
End With
Application.EnableEvents = True
End Sub
End Subthat you should check. It is impossible that you see twoSubwith theeEnd Subon the screen.