Question : EOMONTH, EDATE & NETWORKDAYS alternative

Hi,

Using the attached code I get the error #NAME? on some machines. I think I've pinpointed it down to the fact the the versions of excel range from 2000 - 2003 sp3 and some do not have the analysis toolpack installed?

Is there an alternative I can use for the four lines below?
-------------------------------------------------------------------------------------------------------------------

Range("AS2").Formula = "=EOMONTH(AL2,1)"

Range("A47").Formula = "=AS2-((EOMONTH(AS2,0)-EOMONTH(AS2,-1))-1)"

Range("A48:A58").FormulaR1C1 = "=EDATE(R[-1]C[],-1)"

Range("D47:D58").FormulaR1C1 = "=NETWORKDAYS(RC[-3],EOMONTH(RC[-3],0),Control!R2C13:R23C13)"

-------------------------------------------------------------------------------------------------------------------



What I'm trying to achieve is that that cell AS2 is advanced by plus 1 month then range A47-A58 lists the last 12 months including the current month also range D47:D58 gives the total working days for each of these months.

Thanks in advance
Sq




1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
Private Sub MonthPlus1()

Application.EnableEvents = False

Range("AL2").Formula = "=AS2"
Range("AL2").Value = Range("AL2").Value

Range("AS2").Formula = "=EOMONTH(AL2,1)"
Range("AS2").Value = Range("AS2").Value

Range("AL2").ClearContents
     
    
Range("A47").Formula = "=AS2-((EOMONTH(AS2,0)-EOMONTH(AS2,-1))-1)"
Range("A47").Value = Range("A47").Value

Range("A48:A58").FormulaR1C1 = "=EDATE(R[-1]C[],-1)"
Range("A48:C58").Value = Range("A48:C58").Value

Range("D47:D58").FormulaR1C1 = "=NETWORKDAYS(RC[-3],EOMONTH(RC[-3],0),Control!R2C13:R23C13)"
Range("D47:D58").Value = Range("D47:D58").Value

Application.EnableEvents = True

End Sub

Answer : EOMONTH, EDATE & NETWORKDAYS alternative

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Function Networkdaysvba(startDate As Double, endDate As Date, Optional holidays As Variant) As Integer
    Dim i As Integer
    Dim tmp As Integer
    Dim dt As Date, dtStart As Date
    
    startDate = Fix(startDate)
    tmp = ((endDate - startDate + 1) \ 7) * 5 ' entire work weeks
    dtStart = startDate + (tmp * 7 / 5) ' move to last week
    For dt = dtStart To endDate
        If Weekday(dt, vbMonday) <= 5 Then tmp = tmp + 1 ' add work days in the last week
    Next
    
    If IsArray(holidays) Then
        For Each h In holidays
            ' if any holiday falls in the range, remove it
            If Weekday(h, vbMonday) <= 5 And _
                startDate <= h And endDate >= h Then
                tmp = tmp - 1
            End If
        Next
    End If
    Networkdaysvba = tmp
End Function

Function EOMonthVBA(startDate As Double, months As Long) As Date
    startDate = Fix(startDate)
    EOMonthVBA = DateAdd("m", months + 1, startDate - Day(startDate) + 1) - 1
End Function

Function EDateVBA(startDate As Double, months As Long) As Date
    startDate = Fix(startDate)
    EDateVBA = DateAdd("m", months, startDate)
End Function
Random Solutions  
 
programming4us programming4us