good.learner
good.learner

Reputation: 121

Type mismatch error - Excel VBA

I have the following code. But it is giving me a type mismatch error on Excel 2013 but works fine in Excel 2010.

For Each dateRow In ThisWorkbook.Sheets(1).Range("G8:G153").Cells
    cEndDate = DateValue(dateRow)
    dateArray = Split(cEndDate , "-")
    Day = CInt(dateArray(0)) 'I get the error here
    '<- more code ->
Next

This is working fine with no error in Excel 2010.

Upvotes: 1

Views: 151

Answers (1)

R3uK
R3uK

Reputation: 14537

You should not use Day as variable, because it is a keyword (actually a function!)

And further more, it is the function that you should be using rather than reinventing the wheel! ;)

Dim vDay as Integer
Dim vMonth as Integer
Dim vYear as Integer
Dim dateRow as Range
Dim cEndDate as Date

For Each dateRow In ThisWorkbook.Sheets(1).Range("G8:G153").Cells
    cEndDate = DateValue(dateRow.Value)
    vDay = Day(cEndDate)
    vMonth = Month(cEndDate)
    vYear = Year(cEndDate)


    '<- more code ->
Next dateRow

Upvotes: 2

Related Questions