Pregunta

I have the following code:

For rowIndex = rowOffset To 15
        Dim currentDate As Date
        Dim nextRowDate As Date
        currentDate = Cells(rowOffset, colIndex).Value
        nextRowDate = Cells(rowIndex + 1, colIndex).Value

        Dim currentYear As Date
        currentYear = DatePart("yyyy", currentDate)

        Dim nextYear As Date
        nextYear = DatePart("yyyy", nextRowDate)

        If currentYear <> nextYear Then
            Call RenderYearStyle(rowIndex, colIndex)
        Else
            Call ClearStyle(rowIndex, colIndex)
        End If
    Next rowIndex

Date Part or "Year" functions are not returning the year part of the date. Any ideas?

enter image description here

¿Fue útil?

Solución

Just one correction to your own answer - Year() returns Integer, not String. Bring up Immediate window (Ctrl-G) type ?typename(year(now)) and press enter. VBE is smart that it convert the Integer to String for you in background.

?typename(year(now))
Integer

Otros consejos

the Year() function returns a String not a Date.. DOIIIII.

    Dim currentYear As String
    currentYear = year(currentDate)

    Dim nextYear As String
    nextYear = year(nextRowDate)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top