Thread: Time conversion
View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Time conversion

Sub cc()
Dim hr As Long
Dim mn As Long
For Each C In Range("B5:AF310")
With C
.NumberFormat = "0"
.Value = (Range("A" & C.Row).Value * 60) / _
(Cells(3, C.Column).Value / 60)
.Value = Round(C.Value, 0)
If .Value 60 Then
hr = Int(.Value / 60)
mn = .Value Mod 60
.Value = "'" & hr & ":" & Format(mn, "00")
.NumberFormat = "General"
End If
End With

Next
End Sub

--
Regards,
Tom Ogilvy



"Stuart" wrote in message
...
The following code gives an integer answer representing
minutes:

For Each C In .Range("B5:AF310")
With C
.NumberFormat = "0"
.Value = (Range("A" & C.Row).Value * 60) / _
(Cells(3, C.Column).Value / 60)
.Value = Round(C.Value, 0)
End With
Next

Problem: when the value is 59, I cannot find a 'nice' way to display as
hours and minutes (eg 62 should display
as 1:02)

but I am using this:
If .Value 59 And .Value < 120 Then
i = .Value - 60
If i < 10 Then
.NumberFormat = "General"
strA = "1:0" & i
C.Value = strA
Else
.NumberFormat = "General"
strA = "1:" & i
C.Value = strA
End If
End If

Is there a better way, please?

Regards.