Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在excel中将HHMAM/PM或hmmAM/PM(无空格)格式化为时间hh:mm AM/PM?_Excel_Formatting - Fatal编程技术网

如何在excel中将HHMAM/PM或hmmAM/PM(无空格)格式化为时间hh:mm AM/PM?

如何在excel中将HHMAM/PM或hmmAM/PM(无空格)格式化为时间hh:mm AM/PM?,excel,formatting,Excel,Formatting,我正在研究一种为工资单输入时间表的方法。我希望输入的时间格式为hhmam/PM,没有空格或冒号,最好只键入a/p而不是AM/PM,并将其转换为带冒号和空格的标准时间格式 我搜索了这个社区,找到了一个答案,让我大部分的路。唯一的问题是该代码在输入小时数时需要使用“0”,例如“0545p”转换为“5:45 PM”,但“545p”转换为“54:5 PM” 我希望能够输入0545p或545p,并且仍然可以得到下午5:45的时间 以下是我正在使用的代码: Private Sub Worksheet_Cha

我正在研究一种为工资单输入时间表的方法。我希望输入的时间格式为hhmam/PM,没有空格或冒号,最好只键入a/p而不是AM/PM,并将其转换为带冒号和空格的标准时间格式

我搜索了这个社区,找到了一个答案,让我大部分的路。唯一的问题是该代码在输入小时数时需要使用“0”,例如“0545p”转换为“5:45 PM”,但“545p”转换为“54:5 PM”

我希望能够输入0545p或545p,并且仍然可以得到下午5:45的时间

以下是我正在使用的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
Application.EnableEvents = False
    s = Target.Text
    If Right(s, 1) = "a" Or Right(s, 1) = "A" Then
        s2 = " AM"
    Else
        s2 = " PM"
    End If
    Target.Value = Left(s, 2) & ":" & Mid(s, 3, 2) & s2
Application.EnableEvents = True
端接头

来源问题:


谢谢您的时间。

这是我的答案。。。希望这有助于

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub

'Remarks:
'if you want to use another column for your code just to change this
'Range("A:A") to the desire column
'if you want the imput column G then
'Range("G:G") and the new resulting line of code will be
'If Intersect(Target, Range("G:G")) Is Nothing Then Exit Sub

Application.EnableEvents = False
    s = Target.Text
    If Right(s, 1) = "a" Or Right(s, 1) = "A" Then
        s2 = " AM"
    Else
        s2 = " PM"
    End If
    If Left(s, 2) = "11" Or Left(s, 2) = "12" Or Left(s, 2) = "10" Then
        Target.Value = Left(s, 2) & ":" & Mid(s, 3, 2) & s2
    End If
    If Left(s, 1) = "0" Then
        Target.Value = Left(s, 2) & ":" & Mid(s, 3, 2) & s2
    End If
    If Len(s) = 4 Then
        Target.Value = Left(s, 1) & ":" & Mid(s, 2, 2) & s2
    End If
    'the minutes inside the input (545p | 0545p | 500p)
    'always need to be with 2 digits otherwise will return
    'other hour or smonthing like this 54:5 PM then... the formta input is:
    'hmmA/P
    'hhmmA/P
    'if you want to use 24H format would need another implementation
Application.EnableEvents = True
End Sub

如果你还需要什么,告诉我

检查最左边的字符是1还是0,如果不是,则使用上面的行,然后将其更改为
Target.Value=left(s,1)&“&Mid(s,2,2)&s2
Hi,实际上我对VBE代码一无所知。源问题给了我关于如何复制和通过它的指示。您是要我用您刚才提供的代码替换Target.Value行吗?我还想知道是否有一种方法可以使范围超过一列?例如,如果我想要A列到D列,或者只是A列和C列?每个站点都应该有单独的代码吗?所以不是me站点的代码,所以需要对vba有一些了解。在你不了解的公开论坛上寻求帮助时要小心。有人很容易引导你走上一条会伤害你电脑的路。对此我深表歉意。我仍在努力了解Superuser/StackOverflow/Stack Exchange。这听起来可能很疯狂,但是有没有一个地方可以问关于在哪里提问的问题?我想问10多个企业管理问题,但不确定是否有合适的地方。基本上,我得到的是,我需要创建另一个if参数来检查1或0,然后在左边,如果不是,则放入代码。这似乎对任何事情都很有效。然而,我正在努力理解它。我猜Len(s)=4的最后一个If语句适用于545p选项,对吗?这里我们不放零,所以它只从左边返回一个字符,添加一个分号,然后从右边返回两个字符。为什么我们需要第二个If语句,即检查“11”、“12”和“10”的语句?