我正在office 2016中尝试networkdays循环,但没有通过。
Sub Macro8()
Dim N As Long
Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
N = Sheets("Dump").Range("A" & Rows.Count).End(xlUp).Row
Range("E1") = Date 'i want to use today's date as lastest date to find the networkdays
For i = 2 To N
Cells(i, 4).Formula = wf.NetworkDays(Cells(i, 3), Cells(1, 5))
Next N
End Sub
代码中的问题是没有定义i计数器,当你关闭循环时,应该是"下一个i";。没有必要设置工作表功能。
试试这个:
Sub TEST_NetworkDays()
Dim N As Long, i As Long
N = Sheets("Dump").Range("A" & Rows.count).End(xlUp).row
Range("E1") = Date 'i want to use today's date as lastest date to find the networkdays
For i = 2 To N
Cells(i, 4).Value = Application.WorksheetFunction.NetworkDays(Cells(i, 3), Cells(1, 5))
Next i
'with Holidays
For i = 2 To N
Cells(i, 4).Value = Application.WorksheetFunction.NetworkDays(Cells(i, 3), Cells(1, 5), Range(Cells(3, 9), Cells(12, 9)))
Next i
End Sub