M
MLT
I have the following code that's meant to calculate average times
between open and close dates. But for some reason that I can't figure
out its looping where I don't expect it to loop. I've added MsgBoxes
throughout to try to help but I'm stuck. Does anyone know why this
appears to be looping outside of the for loop?
Function LagAverage(OpenDates As Range, PNum As Range) As Single
MsgBox "starting now"
EOPeriod = Sheets("Hub").Range("B8").Value - 1 + PNum * 28
TimeSum = 0
Counter = 0
For Each ODate In OpenDates
MsgBox ODate.Value
If Not IsEmpty(ODate) Then
MsgBox "past 1"
If ODate.Value <= EOPeriod Then
MsgBox "past 2"
If Not IsEmpty(ODate.Offset(0, 6)) Then
MsgBox "past 3"
If ODate.Offset(0, 6).Value <= EOPeriod Then
TimeSum = TimeSum + (ODate.Offset(0, 6).Value
- ODate.Value)
Counter = Counter + 1
MsgBox Counter
MsgBox TimeSum
ElseIf (EOPeriod - ODate.Value) >= 30 Then
TimeSum = TimeSum + (EOPeriod - ODate.Value)
Counter = Counter + 1
End If
ElseIf (EOPeriod - ODate.Value) >= 30 Then
TimeSum = TimeSum + (EOPeriod - ODate.Value)
Counter = Counter + 1
End If
End If
End If
Next ODate
MsgBox "exited for loop"
AverageDates = TimeSum / Counter
MsgBox TimeSum
MsgBox Counter
LagAverage = AverageDates
End Function
between open and close dates. But for some reason that I can't figure
out its looping where I don't expect it to loop. I've added MsgBoxes
throughout to try to help but I'm stuck. Does anyone know why this
appears to be looping outside of the for loop?
Function LagAverage(OpenDates As Range, PNum As Range) As Single
MsgBox "starting now"
EOPeriod = Sheets("Hub").Range("B8").Value - 1 + PNum * 28
TimeSum = 0
Counter = 0
For Each ODate In OpenDates
MsgBox ODate.Value
If Not IsEmpty(ODate) Then
MsgBox "past 1"
If ODate.Value <= EOPeriod Then
MsgBox "past 2"
If Not IsEmpty(ODate.Offset(0, 6)) Then
MsgBox "past 3"
If ODate.Offset(0, 6).Value <= EOPeriod Then
TimeSum = TimeSum + (ODate.Offset(0, 6).Value
- ODate.Value)
Counter = Counter + 1
MsgBox Counter
MsgBox TimeSum
ElseIf (EOPeriod - ODate.Value) >= 30 Then
TimeSum = TimeSum + (EOPeriod - ODate.Value)
Counter = Counter + 1
End If
ElseIf (EOPeriod - ODate.Value) >= 30 Then
TimeSum = TimeSum + (EOPeriod - ODate.Value)
Counter = Counter + 1
End If
End If
End If
Next ODate
MsgBox "exited for loop"
AverageDates = TimeSum / Counter
MsgBox TimeSum
MsgBox Counter
LagAverage = AverageDates
End Function