Time conversion

EighterFromDecatur

New Member
Joined
Aug 27, 2011
Messages
2
I want to be able to enter minutes and seconds without colon (730 instead of 7:30). My code to convert to a serial time value seems to be working, but I'm having problems formatting the output. When I try to suppress the hour, the minute is always 12. Why?

Sub TimeConv()
Dim str As String, t As Integer, mm As Integer, ss As Integer, mmSerial As Double, ssSerial As Double, tSerial As Double
str = InputBox("Enter a time without punctuation (Example: For 7 minutes, 30 seconds, enter '730'):")
If str <> "" Then t = CLng(str) Else Exit Sub
mm = Int(t / 100)
ss = t - (mm * 100)
mmSerial = mm / 1440
ssSerial = ss / 86400
tSerial = mmSerial + ssSerial
MsgBox "You entered: " & t & vbCr & _
"mm: " & Format(mm, "00") & " ss: " & Format(ss, "00") & vbCr & _
"Serial Minute: " & mmSerial & vbCr & _
"Serial Second: " & ssSerial & vbCr & _
"Serial Time (mm + ss): " & tSerial & vbCr & _
"Format hh:mm:ss: " & Format(tSerial, "hh:mm:ss") & vbCr & _
"Format [hh:]mm:ss: " & Format(tSerial, "[hh:]mm:ss") & vbCr & _
"Format mm:ss: " & Format(tSerial, "mm:ss"), _
vbInformation + vbOKOnly, "Date and Time"
End Sub
 
Last edited:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Maybe a different way:

Code:
Sub TimeConv()
    Dim sTime       As String
    Dim t           As Date
 
    sTime = InputBox("Enter a time without punctuation (e.g., 730 for 0:07:30):")
    If Len(sTime) = 0 Then Exit Sub
 
    On Error Resume Next
    t = Format(sTime, "0:00:00")
    MsgBox IIf(Err.Number, "Invalid", t)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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