“这是我到目前为止所拥有的,将整个列涂成红色”
“试图从 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 列中的单元格。
关于excel - 尝试比较两列中的日期,如果 18 岁或以下将单元格涂成红色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69166743/