yankeek
asked on
VBA formula for converting a sum of total minutes to actual hours/minutes
I am trying to sum up a total of minutes, divide it by 60, and get an actual HOURS:MINUTES result.
I have a total of 315 minutes. Divided that by 60 and the result is 5.25. The .25 is a quart of an hour and I need to get the actual 15 minutes. Does that make sense?
So a total of 315 is actually 5 hours and 15 minutes. That is the result I'm trying to come up with, not .25 as a quarter of an hour.
C'mon, time formula's are fun!
Thanks folks!
Ken
I have a total of 315 minutes. Divided that by 60 and the result is 5.25. The .25 is a quart of an hour and I need to get the actual 15 minutes. Does that make sense?
So a total of 315 is actually 5 hours and 15 minutes. That is the result I'm trying to come up with, not .25 as a quarter of an hour.
C'mon, time formula's are fun!
Thanks folks!
Ken
divide minutes by 1440
You may try something like this...
To call this, you can pass minutes in the function like this...
Function MinutesToHoursAndMinutes(ByVal vMinutes As Long)
Dim strHours As String, strMinutes As String
If vMinutes >= 60 Then
strHours = Int(vMinutes / 60) & " Hours"
strMinutes = (vMinutes Mod 60) & " Minutes"
Else
strHours = "0 Hour"
strMinutes = vMinutes & " Minutes"
End If
MinutesToHoursAndMinutes = strHours & " " & strMinutes
End Function
To call this, you can pass minutes in the function like this...
Sub test()
MsgBox MinutesToHoursAndMinutes(400)
MsgBox MinutesToHoursAndMinutes(Range("A1").Value)
End Sub
Hi,
If you *need* it in VBA, then see above, but if you just want to convert a numeric value of 315 into 5:15, then if A1 = 315:
=A1/1440
Or
=A1/(60*24) - which is probably better as it is more explicit for someone trying to follow the logic
then format the cell with a custom format as required such as:
h:mm (shows as '5:15')
hh:mm (shows as '05:15')
h" hrs "mm" mins" (shows as '5 hrs 15 mins')
The other advantage of this is that the value of the cell (0.21875) is a real time value within Excel, so you can use it easily in other future calculations if required.
Alan.
If you *need* it in VBA, then see above, but if you just want to convert a numeric value of 315 into 5:15, then if A1 = 315:
=A1/1440
Or
=A1/(60*24) - which is probably better as it is more explicit for someone trying to follow the logic
then format the cell with a custom format as required such as:
h:mm (shows as '5:15')
hh:mm (shows as '05:15')
h" hrs "mm" mins" (shows as '5 hrs 15 mins')
The other advantage of this is that the value of the cell (0.21875) is a real time value within Excel, so you can use it easily in other future calculations if required.
Alan.
ASKER
Sorry folks, this is NOT Excell. It is VBA within an Acess DB.
Thanks
Ken
Thanks
Ken
Thanks Ken for responding so quickly :)
Try below:
Try below:
Format(<fieldname>, "hh:mm AMPM")
orInt([fieldname]/60) & ":" & Format([fieldname] Mod 60,"00")
If we start by looking at, and understanding how time works in vba, and noticing that the fractional part of a date variable (which is really more like dateTIME, but hey) expresses the amount of time passed since midnight, and then considering we have 24*60 minutes in a day, we can quite easily convert any given amount of time into hours and minutes by using:
If you want to store this as a date variable, i.e. convert it for storage , then use NumberOfMinutes/(24*60)
Hope that helps.
format(NumberOfMinutes/(24*60),"hh:mm")
'e.g
format(315/(24*60),"hh:mm")
which gives the result "05:15" (a string). This obviously doesn't take into account periods going above a full day.If you want to store this as a date variable, i.e. convert it for storage , then use NumberOfMinutes/(24*60)
Hope that helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Not sure about Access, but in Excel if you format the cell as [h]:mm then it will show the total in hours, even if more than 24 hours.
A1 = 1890
Format A1 as [h]:mm and it displays as 31:30
I would think that it is just as simple to work with hours in Access, but I haven't even opened Access since about 1999, so I could be wrong.
Alan.
Not sure about Access, but in Excel if you format the cell as [h]:mm then it will show the total in hours, even if more than 24 hours.
A1 = 1890
Format A1 as [h]:mm and it displays as 31:30
I would think that it is just as simple to work with hours in Access, but I haven't even opened Access since about 1999, so I could be wrong.
Alan.
ASKER
Alan, no, in Access pure VBA, the hours start over at 24. Excel must be allowing it because it's a cell format.
Thanks
Thanks
Noting again, this is in Excel VBA, the following works:
debug.Print WorksheetFunction.Text(189 0/1440, "[h]:mm")
Gives:
31:30
However, I am guessing that WorksheetFunction.Text won't work in Access?
What about a calculated field (I assume they still exist) - could you do this in VBA:
TimeElapsedCalculated: [TimeElapsedInMins]\60 & Format([TimeElapsedInMins] Mod 60,"\:00")
Alan.
debug.Print WorksheetFunction.Text(189
Gives:
31:30
However, I am guessing that WorksheetFunction.Text won't work in Access?
What about a calculated field (I assume they still exist) - could you do this in VBA:
TimeElapsedCalculated: [TimeElapsedInMins]\60 & Format([TimeElapsedInMins]
Alan.
ASKER
Access VBA gives. See how once it hit 24, the hours started over.. =(
?format(1890/1440, "hh:mm")
07:30
?format(1890/1440, "hh:mm")
07:30
Okay, and I guess using the same format doesn't work either, nor the calculated field?
ASKER
Access doesn't recog "[h]:mm" as a format
Darn!
Sorry - No more ideas.
Alan.
Sorry - No more ideas.
Alan.
ASKER
Figured it out myself