Is it possible to format time in [h]:mm format using VBA?
[h]:mm format in excel would show 25 hours as 25:00, 125 hours as 125:00
I've tried several things such as:
format(datetime, "[h]:mm")
format(datetime, "H:mm")
format(datetime, "hh:mm")
None of these have the desired effect. I've also had a look through the MS help and can't find anything useful.
Use the TEXT worksheet function via the application object, as so:
x = Application.Text(.294,"[h]:mm")
JFC beat me to it, but here's what I came up with anyway...
Sub Test()
Debug.Print FormatHM(24 / 24) '24:00
Debug.Print FormatHM(25 / 24) '25:00
Debug.Print FormatHM(48 / 24) '48:00
Debug.Print FormatHM(48.6 / 24) '48:36
End Sub
Function FormatHM(v As Double) As String
FormatHM = Format(Application.Floor(v * 24, 1), "00") & _
":" & Format((v * 1440) Mod 60, "00")
End Function
not with the format function, but you can use Range(MyData).NumberFormat = "[h]:mm"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With