Home > other >  How to add 15 minutes to date stored as string in VBA
How to add 15 minutes to date stored as string in VBA

Time:02-15

I have tried multiple ways but nothing seems to work.

I have datetime in this format 1.1.2021 10:10 and I would like to add 15 minutes to it so it would look like this 1.1.2021 10:25.

Date time is stored in cell as string and this is what I tried
akt.Offset(0,2) TimeSerial(0, 15, 0)
DateAdd("n", 15, akt.Offset(0,2))
The result is always blank

What am I doing wrong and what is the best way to do it?

CodePudding user response:

You can first make sure the string is converted to a date time value. Then you can add 15 minutes (or 1 / (24*4) of a day):

(using the provided string in cell A1 as example)

my_time = Cells(1, 1).Value

my_new_time = DateValue(Replace(Left(my_time, 8), ".", "/"))   TimeValue(Mid(my_time, 9))   1 / (24 * 4)
MsgBox (my_new_time)

CodePudding user response:

Use CDate:

' True date value:
=DateAdd("n", 15, CDate(Replace(akt.Offset(0,2), ".", "/")))

' Text date:
=Format(DateAdd("n", 15, CDate(Replace(akt.Offset(0,2), ".", "/"))), "d.m.yyyy hh:nn")
  • Related