0

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

4

2 回答 2

1

只需对您自己的答案进行一次更正 -Year()返回整数,而不是字符串。调出立即窗口 (Ctrl-G) 类型?typename(year(now))并按 Enter。VBE 很聪明,它会在后台为您将 Integer 转换为 String。

?typename(year(now))
Integer
于 2013-08-14T01:23:19.857 回答
0

Year() 函数返回一个字符串而不是日期.. DOIIIII。

    Dim currentYear As String
    currentYear = year(currentDate)

    Dim nextYear As String
    nextYear = year(nextRowDate)
于 2013-08-13T22:41:03.727 回答