Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Any suggestions for more efficient time conversion code? 1

Status
Not open for further replies.

PStrongs

Instructor
Oct 30, 2007
30
0
0
GB
Hi,

I have written this procedure that converts decimal time (1.75) into analogue time (1hr 45 mins). It works ok, but seems a little verbose. Any suggestions for making it more efficient and shorter?

Regards,

Code:
Sub testString()

Dim Duration As String
Dim hrs As String, mins As String, totTime as String

'set Duration to decimal time
Duration = "1.75"

If Len(Duration) = 3 Then
	totTime = Duration * 60 & “mins”
		
	ElseIf Len(Duration) = 4 then
	Hrs=Left(Duration, Len(Duration) – 3)
	Mins = Right(Duration, Len(Duration) – 1) * 60

		If hrs = “1” Then
			totTime = hrs & “hr “ & mins & “mins”
		else
			totTime = hrs & “hrs “ & mins & “mins”
		end if

	ElseIf Len(Duration) = 5 Then
	Hrs = Left(Duration, Len(Duration) – 3)
	Mins = Right(Duration, Len(Duration) – 2) * 60
	totTime = hrs & “hrs “ & mins & “mins”

Else
'could be hundreds of hours
Hrs = Left(Duration, Len(Duration) – 3)
Mins = Right(Duration, Len(Duration) – 3) * 60
totTime = hrs & “hrs “ & mins & “mins”

End If

MsgBox totTime

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top