我创建了一个子程序,用于检查列F
中是否发生了更改,并将时间戳写入列G
中的相应单元格。如何通过查找列H中的时间戳与包含周开始日期H
的单元格之间的差异来编辑此子网以返回列A1
中的网络天数?如果没有VBA,公式为=ABS(NETWORKDAYS(A1, B1) - SIGN(NETWORKDAYS(A1, H1))
。以下是我的代码。有帮助吗?
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
答案 0 :(得分:1)
以下是如何在 VBA
中使用 Networkdays()Sub dural()
Dim d1 As Date, d2 As Date, wf As WorksheetFunction
Dim N As Long
Set wf = Application.WorksheetFunction
d1 = DateValue("1/1/2014")
d2 = DateValue("12/31/2014")
N = wf.NetworkDays(d1, d2)
MsgBox N
End Sub
答案 1 :(得分:0)
一旦我清理了你的缩进词,很明显你有一个" End Sub"在"中"中"您的"工作表_变更"中的陈述子。格式化可以帮助您捕捉到这种情况。