Excel VBA Add 1 hour to date / time string

I have a text string that looks like this: "06/10/15 4:53 pm". I want to add 1 hour (exactly 60 minutes) to a text line that looks like this: "06/10/15 5:53 pm"

Any suggestions would be appreciated.

+4
source share
4 answers

Convert it to date, add 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

Link:

+7
source

There is no need for VBA ... provided that the time value above (06/10/15 4:53 pm) is in cell A1 you are looking for:

=A1+TIME(1,0,0)
+1
source

VBA:

s = "06/10/15 4:53pm"

MsgBox CDate(s) + 1 / 24
0

Source: https://habr.com/ru/post/1598107/


All Articles