简体   繁体   中英

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM