【发布时间】:2013-08-07 11:27:56
【问题描述】:
Excel 无法正确识别年份时遇到问题。
这是我的代码
' Sort value
max = Sheets("booking").Cells(Rows.count, "A").End(xlUp).Row
Range("A1:H" & max).Select
ActiveWorkbook.Worksheets("Booking").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Booking").Sort.SortFields.Add Key:=Range("D2:D" & max) _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Booking").Sort.SortFields.Add Key:=Range("A2:A" & max) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Booking").Sort
.SetRange Range("A1:H" & max)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
' mark past booking in italic and in red if not validated
For L = 2 To max
If Format(CDate(booking.Cells(L, 5).Value), "dd-mm-yy") < Format(CDate(cells(1,10).value), "dd-mm-yy") Then
booking.Cells(L, 5).EntireRow.Font.Italic = True
If booking.Cells(L, 5).Offset(0, 3).Value = "" Then
booking.Cells(L, 5).EntireRow.Font.Color = vbRed
Else
booking.Cells(L, 5).EntireRow.Font.Color = vbBlack
End If
End If
Next L
D 列全是日期格式dd-mm-yy
单元格(1,10) = now()
当代码需要按日期对订单进行排序时:np,它将2014年的订单放在2013年的订单前面
但是
当代码对日期应用斜体格式时
我做错了什么?
【问题讨论】:
-
您不应该使用“格式”来比较日期。您只是不必这样做,它可能会一团糟。