Nov 03 2022 02:58 AM
Hello everyone, this is my Princess Leia to Obi-Wan moment right now: you are my only hope.
In a nutshell I'm trying to make a cell convert a whole number to a time format.
Ex: typing 534 in cell E1 turns into 5:34, or 1218 turns into 12:18, all in E1. Bonus points if it adds AM or PM dependent of local time of the computer.
Using the standard cell format "time" just gives me 12:00 AM when I type a whole number everytime.
I'm making a production spreadsheet for my office and part of what we have to write down is our start and end time on different activites. Most of my office isn't super computer literate so I'm trying to make it as easy as possible for them to enter the time, not having to worry about adding the : would help I think as we are all heavy data entry users with whole numbers already.
I'm assuming I'll need to use the VBA code or macro to format the cell to give the outcome I'm looking for but I honestly don't know. I only know VBA code is a thing because of googling the answer to another issue with this spreadsheet lol
Any help or ideas would be great as I've searched all over and haven't quite found the answer to the exact issue I'm looking for here.
Thanks :)
Nov 03 2022 04:10 AM
SolutionYou do need VBA for this.
Let's say you want to be able to enter times as whole numbers in E1:E10.
Apply the desired time format to these cells.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the code listed below into the worksheet module.
Switch back to Excel.
Save the workbook as a macro-enabled workbook (.xlsm).
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
On Error GoTo EndMacro
' Change the range E1:E10 as needed
If Application.Intersect(Target, Range("E1:E10")) Is Nothing Then Exit Sub
If Target.CountLarge > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
If Target.HasFormula Then Exit Sub
Application.EnableEvents = False
With Target
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01 AM
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12 AM
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34:56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
Instruct the users to allow macros when they open the workbook.
Source: Date and Time Entry
Nov 03 2022 04:39 AM
Thank you SO MUCH for this! Works perfectly!
Thank you for being so clear with the instructions on applying the code as well. Very new to using VBA so that was greatly appreciated.
On to the next issue: jumping to a specific cell once I click enter on a different one lol. Still got some Googling and trial and error to do on that one before I come ruining to the forums though lol. Thanks again!
Nov 03 2022 04:10 AM
SolutionYou do need VBA for this.
Let's say you want to be able to enter times as whole numbers in E1:E10.
Apply the desired time format to these cells.
Right-click the sheet tab.
Select 'View Code' from the context menu.
Copy the code listed below into the worksheet module.
Switch back to Excel.
Save the workbook as a macro-enabled workbook (.xlsm).
Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim TimeStr As String
On Error GoTo EndMacro
' Change the range E1:E10 as needed
If Application.Intersect(Target, Range("E1:E10")) Is Nothing Then Exit Sub
If Target.CountLarge > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
If Target.HasFormula Then Exit Sub
Application.EnableEvents = False
With Target
Select Case Len(.Value)
Case 1 ' e.g., 1 = 00:01 AM
TimeStr = "00:0" & .Value
Case 2 ' e.g., 12 = 00:12 AM
TimeStr = "00:" & .Value
Case 3 ' e.g., 735 = 7:35 AM
TimeStr = Left(.Value, 1) & ":" & _
Right(.Value, 2)
Case 4 ' e.g., 1234 = 12:34
TimeStr = Left(.Value, 2) & ":" & _
Right(.Value, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
TimeStr = Left(.Value, 1) & ":" & _
Mid(.Value, 2, 2) & ":" & Right(.Value, 2)
Case 6 ' e.g., 123456 = 12:34:56
TimeStr = Left(.Value, 2) & ":" & _
Mid(.Value, 3, 2) & ":" & Right(.Value, 2)
Case Else
Err.Raise 0
End Select
.Value = TimeValue(TimeStr)
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
Instruct the users to allow macros when they open the workbook.
Source: Date and Time Entry