[英]year n+1 < year n in excel vba
我對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年訂單之前
但
當代碼對日期<現在的日期應用斜體格式時,2014年訂單被視為過時,代碼將其以斜體顯示
我究竟做錯了什么 ?
顯然,在For循環中,您將日期作為字符串進行比較,因此您獲得不同的結果並不感到驚訝。 你應該改變
If Format(CDate(booking.Cells(L, 5).Value), "dd-mm-yy") < Format(CDate(cells(1,10).value), "dd-mm-yy") Then
至
If booking.Cells(L, 5).Value < booking.Cells(1,10) Then
不相關地講, If
語句第二部分中的變量cells
的值是什么? 您忘了先booking.
嗎booking.
?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.