View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jules[_7_] Jules[_7_] is offline
external usenet poster
 
Posts: 3
Default #NAME? when using custom defined function

It is stored in the module1 (is there a possibility to store it elsewhere?)

The code worked and is still the same, so I guess the problem it is not
there?

The code:
Function DiffWeekDays(startdate, starttime, enddate, endtime)
'' returns the nb of hours!
Dim opentime, closetime
opentime = TimeValue("09:00:00")
closetime = TimeValue("17:00:00")
Dim startsecs, endsecs, nb
nb = -1
If IsBusinessDay(startdate) Then
If DateDiff("s", closetime, starttime) 0 Then
' after business hours
startsecs = 0
Else
' started before/in business hours
startsecs = DateDiff("s", opentime, starttime)
If startsecs < 0 Then
' before
startsecs = 8 * 60 * 60
Else
startsecs = 8 * 60 * 60 - startsecs
End If
End If
Else
' Not started on a business day
startsecs = 0
End If

If IsBusinessDay(enddate) Then
If DateDiff("s", closetime, endtime) 0 Then
' after business hours
endsecs = 8 * 60 * 60
Else
' ended before/in business hours
endsecs = DateDiff("s", opentime, endtime)
If endsecs < 0 Then
endsecs = 0
End If
End If
Else
' Not ended on a business day
endsecs = 0
End If
secs = startsecs + endsecs
If secs = 8 * 60 * 60 Then
secs = secs - 8 * 60 * 60
nb = 0
End If
If (DateDiff("s", startdate, enddate) < 0) Or ((DateDiff("s", startdate,
enddate) = 0) And (DateDiff("s", starttime, endtime) < 0)) Then
DiffWeekDays = "ERROR: start after end"
Else
DiffWeekDays = MyDiffWeekDays(startdate, enddate, nb, secs)
End If
End Function

Private Function IsWeekDay(mydate)
Dim myday
myday = Weekday(mydate)
If ((myday = "1") Or (myday = "7")) Then
IsWeekDay = False
Else: IsWeekDay = True
End If
End Function

Private Function IsHoliday(mydate)
Dim holidays, holiday, tmp
tmp = False
holidays = Array("01/01/2000" & _
"", "01/01/2004" & _
"", "09/04/2004" & _
"", "12/04/2004" & _
"", "01/05/2004" & _
"", "20/05/2004" & _
"", "31/05/2004" & _
"", "21/07/2004" & _
"", "15/08/2004" & _
"", "01/11/2004" & _
"", "11/11/2004" & _
"", "24/12/2004" & _
"", "25/12/2004" & _
"", "26/12/2004" & _
"", "31/12/2004" & _
"", "01/01/2005")
For i = 0 To UBound(holidays)
holiday = DateValue(holidays(i))
If DatePart("d", holiday) = DatePart("d", mydate) And DatePart("m",
holiday) = DatePart("m", mydate) And DatePart("y", holiday) = DatePart("y",
mydate) Then
tmp = True
End If
Next
IsHoliday = tmp
End Function

Private Function IsBusinessDay(mydate)
If (Not (IsWeekDay(mydate)) Or IsHoliday(mydate)) Then
IsBusinessDay = False
Else: IsBusinessDay = True
End If
End Function

Private Function MyDiffWeekDays(startdate, enddate, nb, secs)
If DateDiff("d", startdate, enddate) = 0 Then
Dim tmp
tmp = nb * 8 + secs / (60 * 60)
If tmp < 0 Then
MyDiffWeekDays = tmp + 8
Else
MyDiffWeekDays = tmp
End If
Else
If IsBusinessDay(startdate) Then
MyDiffWeekDays = MyDiffWeekDays(DateAdd("d", 1, startdate), enddate,
nb + 1, secs)
Else
MyDiffWeekDays = MyDiffWeekDays(DateAdd("d", 1, startdate), enddate,
nb, secs)
End If
End If
End Function

"Bob Phillips" wrote in message
...
Where is the function stored?

What is the code for somecalculatedvalue?

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jules" wrote in message
...
In Module1 of an xls file, I added a function like:

Function DiffWeekDays(a,b)
DiffWeekDays = somecalculatedvalue
End Function

I use this function in some cells and it works perfect.

However after some days, I open the file again.
In the cells I used the function, I see now: #NAME?
The function hasn't been changed in the meanwhile.

Has anybody some idea what's causing this?

Many thanks in advance!

best regards,
Joris.