Search code examples
vbaworksheet-function

Networkdays Loop - I Want to get the Networkdays loop . but giving an error. I am using office 2016. appreciate your efforts in advance


i am trying networkdays loop in office 2016 but not getting through.

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

Solution

  • The problem in your code is the i counter wasn't defined and when you're closing the loop should be "Next i". It's not necessary to set the worksheetfunction.

    Try this:

    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