Hi all,
I'm trying to write to the referenced cell Myrange.Value on the last line. The message box does display the correct value.
Thanks In advance for the help.
Daniel
I'm trying to write to the referenced cell Myrange.Value on the last line. The message box does display the correct value.
Thanks In advance for the help.
Daniel
Code:
Option Compare Text
Function formatHours(Myrange As Range) As String
Dim strInput, strPattern, StartPeriod, EndPeriod As String
Dim StartHour, StartMinute, EndHour, EndMinute, BreakLength As Integer
Dim RegEx: Set RegEx = New RegExp
With RegEx
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = strPattern
End With
RegEx.Pattern = "^(\d+):?(\d+)?(AM|PM)? (\d+):?(\d+)?(AM|PM)? ?(\d+)?"
strInput = Myrange.Value
Set matches = RegEx.Execute(strInput)
If matches.Count <> 0 Then
StartMinute = 0
StartPeriod = "AM"
EndMinute = 0
EndPeriod = "PM"
BreakLength = 0
StartHour = matches(0).SubMatches(0)
If matches(0).SubMatches(1) <> "" Then
StartMinute = matches(0).SubMatches(1)
End If
If matches(0).SubMatches(2) <> "" Then
StartPeriod = matches(0).SubMatches(2)
End If
If StartPeriod = "PM" Then
StartHour = StartHour + 12
End If
EndHour = matches(0).SubMatches(3)
If matches(0).SubMatches(4) <> "" Then
EndMinute = matches(0).SubMatches(4)
End If
If matches(0).SubMatches(5) <> "" Then
EndPeriod = matches(0).SubMatches(5)
End If
If matches(0).SubMatches(6) <> "" Then
BreakLength = matches(0).SubMatches(6)
End If
If EndPeriod = "PM" Then
EndHour = EndHour + 12
End If
StartTime = TimeSerial(StartHour, StartMinute, 0)
EndTime = TimeSerial(0, ((EndHour * 60) + EndMinute) - BreakLength, 0)
formatHours = Round((DateDiff("n", StartTime, EndTime) / 60), 2)
EndTime = TimeSerial(EndHour, EndMinute, 0)
MsgBox (Left(StartTime, 5) & "-" & Left(EndTime, 5))
Myrange.Value = Left(StartTime, 5) & "-" & Left(EndTime, 5)
End If
End Function