Search code examples
vbaexcelexcel-2010excel-2007

Excel VBA Convert String Date to a real date


I'm using the following code but for some reason the second line isn't converting it into a real date.

DateStr = Replace(DateStr, "-", "/")
DateStr = Format(CDate(DateStr), "dd/mm/yyyy")
DateStr = DateStr + 1

Even if I do:

Dim RealDate As Date

DateStr = Replace(DateStr, "-", "/")
RealDate = Format(CDate(DateStr), "dd/mm/yyyy")
RealDate = RealDate + 1

As you can see from line 3, I am trying to +1 to the day which could also change the month or year potentially.


Solution

  • You are having trouble identifying just where you should be adding 1 to the date. Try,

    DateStr = Format(CDate(DateStr) + 1, "dd/mm/yyyy")
    

    The CDate conversion function is where you get the numerical date vaue that will accept the addition of another day.