跳到主要内容

如何在数字之间插入冒号以使其在Excel中自动成为时间格式?

当您在工作表单元格中插入时间格式时,您是否曾经每次输入冒号都感到烦恼? 在本文中,我将讨论在输入数字以使其在Excel中自动成为时间格式时如何插入冒号。

在数字之间插入冒号,使它们成为带有公式的时间格式

使用VBA代码输入数字时,自动将数字设置为时间格式


箭头蓝色右气泡 在数字之间插入冒号,使它们成为带有公式的时间格式

假设您有一个数字列表,可以插入冒号并将其设置为时间格式,以下公式可以帮您一个忙。

如果数字始终是3或4位数字,则可以使用以下公式:

=TIME(LEFT(A1,LEN(A1)-2),RIGHT(A1,2),0),然后将填充手柄向下拖动到要包含此公式的单元格,您将根据需要获得以下结果:

doc将冒号插入数字1

如果数字是5或6位数字,请应用以下公式:

=(INT(A1/10000)&":"&INT(MOD(A1,10000)/100)&":"&MOD(A1,100))+0,然后将填充手柄向下拖动到要包含此公式的单元格,您将获得数字序列,如以下屏幕截图所示:

doc将冒号插入数字2

然后,应通过单击将数字格式化为时间格式 时间 来自 总类 下的下拉列表 主页 标签,请参见屏幕截图:

doc将冒号插入数字3


箭头蓝色右气泡 使用VBA代码输入数字时,自动将数字设置为时间格式

以下VBA代码还可以帮助您在输入数字时将数字自动转换为时间格式,请按以下步骤操作:

1。 转到要自动在数字中插入冒号的工作表。

2。 右键单击工作表标签,然后选择 查看代码 从上下文菜单中,弹出 Microsoft Visual Basic应用程序 窗口,请复制以下代码并将其粘贴到空白处 模块 窗口,请参见屏幕截图:

VBA代码:自动将数字设置为时间格式:

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Updateby Extendoffice 20160606
    Dim xStr As String
    Dim xVal As String
    On Error GoTo EndMacro
    If Application.Intersect(Target, Range("A1:A20")) Is Nothing Then Exit Sub
    If Target.Count > 1 Then Exit Sub
    If Target.Value = "" Then Exit Sub
    Application.EnableEvents = False
    With Target
        If Not .HasFormula Then
            xVal = .Value
            Select Case Len(xVal)
                Case 1 ' e.g., 1 = 00:01 AM
                    xStr = "00:0" & xVal
                Case 2 ' e.g., 12 = 00:12 AM
                    xStr = "00:" & xVal
                Case 3 ' e.g., 735 = 7:35 AM
                    xStr = Left(xVal, 1) & ":" & Right(xVal, 2)
                Case 4 ' e.g., 1234 = 12:34
                    xStr = Left(xVal, 2) & ":" & Right(xVal, 2)
                Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
                    xStr = Left(xVal, 1) & ":" & Mid(xVal, 2, 2) & ":" & Right(xVal, 2)
                Case 6 ' e.g., 123456 = 12:34:56
                    xStr = Left(xVal, 2) & ":" & Mid(xVal, 3, 2) & ":" & Right(xVal, 2)
                Case Else
                    Err.Raise 0
            End Select
            .Value = TimeValue(xStr)
        End If
    End With
    Application.EnableEvents = True
    Exit Sub
EndMacro:
    MsgBox "You did not enter a valid time"
    Application.EnableEvents = True
End Sub

doc将冒号插入数字4

请注意: 在上面的代码中, A1:A20 是您想在以后插入时间的单元格,您可以根据需要更改单元格引用。

3。 现在,当您在单元格区域A102319:A1中输入诸如20之类的数字时,然后按 输入 键,数字将根据需要自动格式化为10:23:19 AM时间格式。

doc将冒号插入数字5

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (7)
Rated 4.5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
I know this is a very old post, but I am experiencing an error when I try to enter time between 00001 and 005959. It's either converting it to 1AM or throwing an error. Any ideas?
This comment was minimized by the moderator on the site
DZIEKUJE ZA WYJAŚNIENIE, CZY KTOŚ MOŻE PODPOWIEDZIEC CZY DA SIE TO PRZEROBIC TAK ŻEBY DZIŁAŁO NA 2 LUB WIECEJ KOLUMNACH? CHODZI O WPROWADZENIE STARTU PRACY W KOLUMNIE C A ZAKONCZENIA W KOLUMNIE D, WYNIK WYSWIETLI SIE W KOLUMNIE E
Rated 4.5 out of 5
This comment was minimized by the moderator on the site
does anyone know how to make this macro run on google sheets please?
This comment was minimized by the moderator on the site
In an earlier version of windows I was able to change the colon time delimiter to another character (usually an asterisk) to make it easier and faster to enter lots of time data. Is there still a way to do this or has it been "new & improved" into oblivion?
This comment was minimized by the moderator on the site
如果只要時跟分以及24小時制,請問可以怎麼改?
This comment was minimized by the moderator on the site
Hi, kyra,
你可以嘗試使用下面的vba來實現: (只顯示時和分)

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim xStr As String
Dim xVal As String
On Error GoTo EndMacro
If Application.Intersect(Target, Range("A1:A20")) Is Nothing Then Exit Sub
If Target.Count > 1 Then Exit Sub
If Target.Value = "" Then Exit Sub
Application.EnableEvents = False
With Target
If Not .HasFormula Then
xVal = .Value
Select Case Len(xVal)
Case 1 ' e.g., 1 = 00:01 AM
xStr = "00:0" & xVal
Case 2 ' e.g., 12 = 00:12 AM
xStr = "00:" & xVal
Case 3 ' e.g., 735 = 7:35 AM
xStr = Left(xVal, 1) & ":" & Right(xVal, 2)
Case 4 ' e.g., 1234 = 12:34
xStr = Left(xVal, 2) & ":" & Right(xVal, 2)
Case 5 ' e.g., 12345 = 1:23:45 NOT 12:03:45
xStr = Left(xVal, 1) & ":" & Mid(xVal, 2, 2) & ":" & Right(xVal, 2)
Case 6 ' e.g., 123456 = 12:34:56
xStr = Left(xVal, 2) & ":" & Mid(xVal, 3, 2) & ":" & Right(xVal, 2)
Case Else
Err.Raise 0
End Select
.Value = Format(TimeValue(xStr), "Short Time")
End If
End With
Application.EnableEvents = True
Exit Sub
EndMacro:
MsgBox "You did not enter a valid time"
Application.EnableEvents = True
End Sub
This comment was minimized by the moderator on the site
Thank you. Very well illustrated.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations