如何将hhmmam / PM(无空格)格式化为hh:mm AM / PM in excel? [英] How can I format an hhmmAM/PM (no space) to time hh:mm AM/PM in excel?
问题描述
这是一个代码为 A 的小型宏。它可以修改为处理任何一组单元格。该宏监视列 A 中的条目,并将它们从 0745a 或 0745p 等值更改为正常时间。在工作表代码区域中输入以下事件宏:
Private Sub Worksheet_Change(ByVal Target As Range)
如果相交(Target,Range(A:A))Is Nothing Then Exit Sub
Application.EnableEvents = False
s = Target.Text
如果Right(s,1)=a或右(s,1)=A然后
s2 =AM
Else
s2 =PM
End If
Target.Value = Left s,2)& :&中(3,2)& s2
Application.EnableEvents = True
End Sub
因为它是工作表代码,这很容易安装和自动使用:
- 右键单击Excel窗口底部附近的选项卡名称
- 选择查看代码 - 这将打开一个VBE窗口
- 将东西粘贴并关闭VBE窗口
如果您有任何疑虑,请先试用试用版。
如果保存工作簿,则宏将与它一起保存
如果您在2003年之后使用Excel版本,则必须将
保存为.xlsm而不是.xlsx
要删除宏:
- 打开上面的VBE窗口
- 清除代码
- 关闭VBE窗口
要了解有关宏的更多信息,请参阅:
http://www.mvps.org/dmcritchie/ excel / getstarted.htm
和
http://msdn.microsoft.com/en-us/library/ee814735(v = office .14).aspx
要了解有关事件宏(工作表代码)的更多信息,请参阅:
http://www.mvps.org/dmcritchie/excel/event.htm
必须启用宏才能使其工作!
I'm working on a payroll project and to make data entry more efficient I want the time entered in a format with hhmmAM/PM, no spaces or colons and preferable just typing a/p instead of AM/PM and have that convert to standard time format with colons and spaces. Thanks!
Here is a small macro that is coded for column A. It can be modified to handle any set of cells. The macro monitors entries in column A and and changes them from values like 0745a or 0745p into normal times. Enter the following event macro in the worksheet code area:
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
End Sub
Because it is worksheet code, it is very easy to install and automatic to use:
- right-click the tab name near the bottom of the Excel window
- select View Code - this brings up a VBE window
- paste the stuff in and close the VBE window
If you have any concerns, first try it on a trial worksheet.
If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx
To remove the macro:
- bring up the VBE windows as above
- clear the code out
- close the VBE window
To learn more about macros in general, see:
http://www.mvps.org/dmcritchie/excel/getstarted.htm
and
http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Macros must be enabled for this to work!
这篇关于如何将hhmmam / PM(无空格)格式化为hh:mm AM / PM in excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!