vba 如何检查Excel中的日期单元格是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33421574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:03:59  来源:igfitidea点击:

How to check if a date cell in Excel is empty?

excelvbaexcel-vbadateis-empty

提问by Malin

If feels like this should be really easy but I dont get it to work without retrieving the value of the cell again.

如果感觉这应该很容易,但如果不再次检索单元格的值,我就无法让它工作。

To start with, I have 2 date cells:

首先,我有 2 个日期单元格:

Dim agreedDate As Date
Dim completedDate As Date

THIS WORKS .. (but looks messy)

这有效..(但看起来很乱)

agreedDate = Worksheets("Data").Cells(Counter, 7).Value
completedDate = Worksheets("Data").Cells(Counter, 9).Value

If (IsEmpty(Worksheets("Data").Cells(Counter, 7).Value) = True) Or (IsEmpty(Worksheets("Data").Cells(Counter, 9).Value) = True) Then

[.. do stuff]
End If

THIS DOES NOT WORK - WHY NOT?!

这行不通 - 为什么不行?!

agreedDate = Worksheets("Data").Cells(Counter, 7).Value
completedDate = Worksheets("Data").Cells(Counter, 9).Value

If (IsEmpty(agreedDate) = True) Or IsEmpty(completedDate) = True) Then

[.. do stuff]
End If

Is there a way to write the if statement in a clean and easy way?

有没有办法以简洁的方式编写 if 语句?

回答by Excel Hero

Since only variables of type Variant can be Empty, you need a different test for Date types.

由于只有 Variant 类型的变量可以为 Empty,因此您需要对 Date 类型进行不同的测试。

Check for zero:

检查零:

If agreedDate = 0 Or completedDate = 0 Then

But a safer path would be to change the variables to type Variant and then do this test:

但更安全的方法是将变量更改为 Variant 类型,然后执行此测试:

If IsDate(agreedDate) = False Or IsDate(completedDate) = False Then

回答by Malin

The IsEmpty functiondetermines indicated whether a variable has been initialized. If a cell is truly blank then it is considered uninitialized from the IsEmptystandpoint. However, declaring a variable in VBA gives it a default value. In this case the date type variables are essentially 0or 30-Dec-1899 00:00:00as demonstrated by the following short snippet.

IsEmpty函数确定指示变量是否已经被初始化。如果一个单元格真的是空白的,那么从IsEmpty 的角度来看,它被认为是未初始化的。但是,在 VBA 中声明一个变量会给它一个默认值。在这种情况下,日期类型变量本质上是030-Dec-1899 00:00:00,如以下短片所示。

Sub date_var_test()
    Dim dt As Date
    Debug.Print Format(dt, "dd-mmm-yyyy hh:mm:ss")
End Sub

If you want to 'tidy up' your code, you might also wish to allow the true boolean return of the IsEmpty function to resolve the boolean condition rather than comparing it to True.

如果您想“整理”您的代码,您可能还希望允许 IsEmpty 函数的真正布尔值返回来解析布尔条件,而不是将其与True进行比较。

If IsEmpty(Worksheets("Data").Cells(Counter, 7)) Or IsEmpty(Worksheets("Data").Cells(Counter, 9)) Then
    [.. do stuff]
End If

Given that Falseis (for all intents and purposes) zero then this will work for your date type variables.

鉴于False是(出于所有意图和目的)零,那么这将适用于您的日期类型变量。

If Not (agreedDate or completedDate) Then
    [.. do stuff]
End If

回答by u8it

As Excel Hero pointed out, a date variable cannot be empty. In fact, a date variable is a number, so you should be able to do something like this. Also, notice that the code below uses "Value2".

正如 Excel Hero 所指出的,日期变量不能为空。事实上,日期变量是一个数字,所以你应该能够做这样的事情。另外,请注意下面的代码使用“Value2”。

Sub test()
    Dim d As Date
    d = Range("A1").Value2
    If d = 0 Then
      MsgBox "ok"
    Else
      MsgBox "not ok"
    End If
End Sub