从Excel单元格值不起作用的日期开始获取年份
我有以下代码:从Excel单元格值不起作用的日期开始获取年份,excel,vba,datepart,Excel,Vba,Datepart,我有以下代码: 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
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
对于rowIndex=rowOffset到15
将当前日期设置为日期
Dim nextRowDate As Date
currentDate=单元格(行偏移量,共索引)。值
nextRowDate=单元格(行索引+1,共索引)。值
将当前年份设置为日期
currentYear=DatePart(“yyyy”,currentDate)
Dim nextYear作为日期
nextYear=DatePart(“yyyy”,nextRowDate)
如果当前年份为下一年,则
调用RenderEarStyle(行索引、共索引)
其他的
调用ClearStyle(行索引、共索引)
如果结束
下一行索引
日期部分或“年”函数不返回日期的年部分。有什么想法吗
年()函数返回的字符串不是日期。。多伊ii
Dim currentYear As String
currentYear = year(currentDate)
Dim nextYear As String
nextYear = year(nextRowDate)
只需对您自己的答案进行一次更正-
Year()
返回整数,而不是字符串。打开即时窗口(Ctrl-G)键入?键入名称(年份(现在))
,然后按enter键。VBE很聪明,它可以在后台将整数转换为字符串
?typename(year(now))
Integer
我还不能;)(2天)