这是我目前所做的,将整个列都涂成红色
"试图从M列中减去K列中的日期,如果小于18年,表示未成年,则K列中的颜色单元格为红色。">
Dim N As Long, i As Long
Dim NewDate As Date
N = Cells(Rows.Count, "K").End(xlUp).Row
NewDate = DateAdd("yyyy", -18, Date)
For i = 2 To N
If NewDate < Cells(i, "M").Value Then
Cells(i, "K").Interior.Color = RGB(255, 0, 0)
End If
Next i
这是使用DateSerial
的一种方法:
For i = 2 To N
Dim OldDate As Date
OldDate = Cells(i, "K").Value
If DateSerial(Year(OldDate) + 18, Month(OldDate), Day(OldDate)) > Cells(i, "M").Value Then
Cells(i, "K").Interior.Color = vbRed
End If
Next
这也可以通过条件格式来实现,使用基于公式的规则:
=M2<DATE(YEAR(K2)+18,MONTH(K2),DAY(K2))
应用于从第2行开始的K列中的单元格。