我试图将今天的日期与计划中的日期进行比较以突出当前任务。
Sub tasks()
Dim task As Range
Dim i As Integer
Dim debut As Date, fin As Date, today As Date
today = (Date)
With Worksheets("Gantt")
For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row
debut = (.Cells(i, 2).Value)
fin = (.Cells(i, 3).Value)
Set task = Range(.Cells(i, 1), .Cells(i, 5))
If (debut <= today) & (today <= fin) Then
'task.Interior.ColorIndex = 8
Debug.Print "today = " & today
Debug.Print "debut = " & debut
Debug.Print "fin = " & fin
End If
Next i
End With
End Sub
我的代码给了我一个
incompatibility type
我的 if
中的错误陈述。我尝试使用 CBdl
将所有日期转换为 double 类型但无论如何它都会给我同样的错误。表格中的日期是“长日期”,我将它们写为
20/02/2020
例如。有谁知道我错过了什么?先感谢您!
最佳答案
您正在使用 &
而不是 And
这是试图连接两个日期,而不是在逻辑上比较这两个操作。
用。。。来代替
If (debut <= today) And (today <= fin) Then
关于excel - 日期之间的类型不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60392013/