Formatting To Time

Johnboy

Board Regular
Joined
Oct 25, 2004
Messages
144
Hi All,

To format text in cell with macro.

Macro has input box and ask user to input time without colons...

Range("D7").Select
ActiveCell.FormulaR1C1 = InputBox("Enter Start Time Without Colons HHMMSS ie 151725", "Time")

In cell E7 I have the following formula to add colons so excel recognizes as time.

TEXT(D7,"00\:00\:00")

What I would like is to eliminate cell E7 and have the macro format to time with colons in D7.
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
One way:

Code:
    Dim s As String
    
    s = InputBox("Enter Start Time Without Colons HHMMSS ie 151725", "Time")
    If Not s Like "######" Then
        MsgBox "Invalid entry"
        Exit Sub
    End If
    Range("D7").Value = TimeValue(Format(s, "00:00:00"))
 
Upvote 0
Trevor G - your solution did format the cell but not the text entered.

Shg - Works great... is there a way to loop back to input if invalid entry instead of exiting


Thanks
Johnboy
 
Upvote 0
Code:
    Do
        s = InputBox("Enter Start Time Without Colons HHMMSS ie 151725", "Time")
        If s Like "######" Then Exit Do
        MsgBox "Invalid entry"
    Loop
There would need to be more checking to ensure valid times.
 
Upvote 0
shg - Thanks for the help... this works great. I understand the checking is not set for time, but this works for now... Again Thanks.

Johnboy
 
Upvote 0

Forum statistics

Threads
1,224,603
Messages
6,179,850
Members
452,948
Latest member
UsmanAli786

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