View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Niek Otten
 
Posts: n/a
Default UDF's using other UDF's

You are changing input parameters in Timediff, like BeginTime. If you add a
watch for Ws in FindCase, you can see it changing.

a. Better not do that!
b. Changing the definition of the arguments to ByVal instead of the default
ByRef causes the function to return something, but I don't know enough of
what you're trying to do to say anything about the validity of the result.
ex:

Function TimeDiff(ByVal BeginTime As Integer, ByVal EndTime As Integer) As
Single

BTW I'd always use Long instead of Integer and Double instead of Single:
a. to avoid overflow as much as possible
b. to make optimal use of the 32-bit architecture of your processor

--
Kind regards,

Niek Otten


"millsy" wrote in
message ...

The change makes no difference other than to highlight the lack of a
declaration. I agree with your advice but I don't see how it was going
to make any difference anyway. Here's the revised code with all
variable declared. I also changed one of the if statements but that
isn't relevant to the error:

Option Explicit


Function TimeDiff(BeginTime As Integer, EndTime As Integer) As Single

Dim BeginHour As Integer, BeginMinute As Integer
Dim EndHour As Integer, EndMinute As Integer

If EndTime < BeginTime Then EndTime = EndTime + 2400

BeginHour = Int(BeginTime / 100)
BeginMinute = BeginTime - (BeginHour * 100)
BeginTime = (BeginHour * 60) + BeginMinute

EndHour = Int(EndTime / 100)
EndMinute = EndTime - (EndHour * 100)
EndTime = (EndHour * 60) + EndMinute

TimeDiff = (EndTime - BeginTime) / 60

End Function



Function FindCase(Ws As Integer, We As Integer, Rs As Integer, Re As
Integer) As Single
Dim WsRs As Single, RsWe As Single, WsWe As Single, WsRe As Single,
RsWs As Single, WeRe As Single, RsRe As Single, ReWe As Single

WsRs = TimeDiff(Ws, Rs)
RsWe = TimeDiff(Rs, We)
WsWe = TimeDiff(Ws, We)
WsRe = TimeDiff(Ws, Re)
RsWs = TimeDiff(Rs, Ws)
RsRe = TimeDiff(Rs, Re)
WeRe = TimeDiff(We, Re)

FindCase = 0

If WsRs + RsWe = WsWe Then FindCase = FindCase + 1
If RsWs + WsWe + WeRe = RsRe Then FindCase = FindCase + 10
If WsRe + ReWe = WsWe Then FindCase = FindCase + 100
If WsRs + RsRe + ReWe = WsWe Then FindCase = FindCase + 1000

End Function


--
millsy
------------------------------------------------------------------------
millsy's Profile:
http://www.excelforum.com/member.php...fo&userid=5122
View this thread: http://www.excelforum.com/showthread...hreadid=494261