Time conversion

daverunt

Well-known Member
Joined
Jul 9, 2009
Messages
1,946
Office Version
  1. 2013
Platform
  1. Windows
I'd like to convert a time string. I'm using a macro to extract the string from a text file and need to modify the string format within the macro.
Modifying the target cell to a time format is not possible.

hhh:mm:ss - this is always the number of characters
to text format that excludes empty values

For example 000:05:00
Would output 5m

001:10:08
Would output 1h 10m 8s



Any help appreciated.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
If the hours will always be less than 100...

=TEXT(0+MID(A1,2,10),"[h]\h m\m s\s")
 
Upvote 0
VBA Code:
TimeString = Evaluate("TEXT(0+MID(A1,2,10),""[h]\h m\m s\s"")")
 
Upvote 0
I'll give that a try in the morning.
Thanks Johnny.
 
Upvote 0
I'll give that a try in the morning.
Thanks Johnny.
Sorry this didn't work. The time value e.g. 001:01:01 is in a string variable and needs to be converted and held in another string variable as 1h 1m 1s. Nothing is stored in a cell or can be stored in a cell.
 
Upvote 0
Give this function (which you can call from within your macro code) a try. Just pass in a variable containing your specialized time string or the quoted time string itself and the function will return the formatted time string.
VBA Code:
Function FTime(ByVal S As String) As String
  Dim Arr As Variant
  Arr = Split(S, ":")
  FTime = Application.Trim(IIf(Arr(0), Arr(0) & "h ", "") & IIf(Arr(1), Arr(1) & "m ", "") & IIf(Arr(2), Arr(2) & "s", ""))
End Function
Note: I named the function FTime which stands for "Formatted Time".
 
Last edited:
Upvote 0
Cheers Rick. That's awesome.

I added a bit of code found here to remove the leading zero from each arr.

I understand what they did here doing a left trim of spaces and replacing any zeros converted to a space on the right back to zero. Clever.
09m 59s comes out as 9m 59s, which is my preferred output.
  • replace the zeros with spaces
  • left trim
  • replace the spaces with zeros

FTime = Application.Trim(IIf(Arr(0), Replace(LTrim(Replace(Arr(0), "0", " ")), " ", "0") & "h ", "") & IIf(Arr(1), Replace(LTrim(Replace(Arr(1), "0", " ")), " ", "0") & "m ", "") & IIf(Arr(2), Replace(LTrim(Replace(Arr(2), "0", " ")), " ", "0") & "s", ""))

Thanks for your time.
 
Upvote 0
Whoops! I forgot about the leading zeroes (it was 4:30 in the moring when I posted that function). Here is my code modified to remove them...
VBA Code:
Function FTime(ByVal S As String) As String
  Dim Arr As Variant
  Arr = Split(S, ":")
  FTime = Application.Trim(IIf(Arr(0), CLng(Arr(0)) & "h ", "") & IIf(Arr(1), CLng(Arr(1)) & "m ", "") & IIf(Arr(2), CLng(Arr(2)) & "s", ""))
End Function
I will note that my code (this one and the previous one) do not check if the minutes and seconds are less than 60, but I figured that is okay because those time values are being created elsewhere in your code and I figure they are tested there for correctness.
 
Upvote 0
Solution
Cheers Rick. You mean it was 04:30 : )
A lot shorter than the code I used. Tested and works great.
Yes the time comes from a text file generated by a current measurement application and I am copying information to a different file where the format is as requested.

Many Thanks
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,217
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top