Search code examples
vbadatedateadd

VBA String to Date type mismatch


apologies if this has been asked before, I've been searching and can't seem to find an answer that's helping.

I'm currently trying to write an excel macro that will build my work's schedule for me based on information from a 'setup' sheet.

I seem to be having issues with grabbing the 'start date' from the setup sheet, and adding 1 day to it and inputting it into cells. I keep getting a type mismatch.

The cell in setup is P1, and is '2018-02-09' with formatting of 'MMMM dd, yyyy' so it says February 9th, 2018.

I'm using the following code to try and add a day to it:

Dim d As Date
    d = DateValue(startDate)
    d = DateAdd("d", 1, d)
    MsgBox d
    index = index + 1

I've also tried just using DateAdd with startDate without using DateValue, still type mismatch, I've tried just + 1 onto the startDate value, still type mismatch. I've tried reformatting the original value thinking that it was the formatting that fucked it up, still no luck.

Just trying to convert something into a date and add 1 day lol


Solution

  • This is how I did it with a cell, matching your format:

    Dim startDate As String
    startDate = ThisWorkbook.ActiveSheet.Cells(1, 1).Value
    
    Dim datePlusOne As Date
    datePlusOne = DateAdd("d", 1, CDate(startDate))
    MsgBox datePlusOne