[英]Excel VBA Adding 1 hour to a date / time string
I have a text string that looks like this: "06/10/15 4:53pm". 我有一个看起来像这样的文本字符串:“ 06/10/15 4:53 pm”。 I want to add 1 hour (60 minutes exactly) to the text string looks like this: "06/10/15 5:53pm"
我想在文本字符串中添加1小时(准确地说是60分钟),如下所示:“ 06/10/15 5:53 pm”
Any suggestions would be appreciated. 任何建议,将不胜感激。
Convert it to a date, add an hour, and convert back to string using format 将其转换为日期,添加一个小时,然后使用格式转换回字符串
Private Sub TestIt()
MsgBox AddHour("06/10/15 4:53pm")
End Sub
Public Function AddHour(ByVal sTime As String) As String
Dim dt As Date
dt = CDate(sTime)
dt = DateAdd("h", 1, dt)
AddHour = Format(dt, "mm/dd/yy h:nnam/pm")
End Function
Reference: 参考:
不需要VBA ...假设上方(06/10/15 4:53 pm)的时间值在单元格A1中,则您要查找的公式为:
=A1+TIME(1,0,0)
Since you asked for a VBA solution: 由于您要求使用VBA解决方案:
s = "06/10/15 4:53pm"
MsgBox CDate(s) + 1 / 24
For operations with date and time in VBA is DateAdd function. 对于VBA中具有日期和时间的操作,请使用DateAdd函数。 You can find how to use it in this link: https://www.techonthenet.com/excel/formulas/dateadd.php
您可以在此链接中找到如何使用它: https : //www.techonthenet.com/excel/formulas/dateadd.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.