excel - 日期之间的类型不兼容

标签 excel vba

我试图将今天的日期与计划中的日期进行比较以突出当前任务。

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/

相关文章:

vba - 如何使用VBA编写VBA代码

java - 部署到 weblogic

c# - 用C#读取Excel文件——选择工作表

vba - Excel VBA从日期对象获取月份

excel - Excel VBA中mac的 sleep 等效项

excel - 从 Google Sheets 脚本中解析 Excel 文件中的数据

vba - 循环中的帧的名称

excel - 动态地将系列添加到图表中

excel - 如何更改地址属性的 'Column' 部分

vba - Application.Interactive = True 失败