vba Date 变量类型的默认值是否有一个常量,字符串类型是否有一个 vbNullString ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11236929/
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
Is there a const for the default value of the Date variable type, a la vbNullString for the string type?
提问by Swiftslide
I have a column of dates. These will be incorporated into objects, one per row, along with other data in the row.
我有一列日期。这些将被合并到对象中,每行一个,以及该行中的其他数据。
However, some of the cells in the date column are empty. As I test I create a date variable and set its value as the value of an empty cell. I msgbox'd the new value of the date and found it was "12:00 AM". Is there a constant built into VBA to represent this value, so I can do tests similar to:
但是,日期列中的某些单元格是空的。在我测试时,我创建了一个日期变量并将其值设置为一个空单元格的值。我 msgbox'd 日期的新值,发现它是“12:00 AM”。VBA 中是否有一个常量来表示这个值,所以我可以做类似的测试:
If myDate = vbNullDate Then
回答by alex957
I run into this problem from time to time. I usually do the below:
我不时遇到这个问题。我通常执行以下操作:
dim d as date
if d = cdate(0) then msgbox "Default value"
回答by JimmyPena
The default value for a local Date variable seems to be:
本地 Date 变量的默认值似乎是:
Saturday, December 30, 1899 12:00:00 AM
Saturday, December 30, 1899 12:00:00 AM
based on this procedure:
基于此程序:
Sub testdate()
Dim d As Date
Debug.Print Format$(d, "Long Date") & " " & Format$(d, "Long Time")
End Sub
To check the default value for any variable type, simply declare a variable of that type, then print it's value.
要检查任何变量类型的默认值,只需声明该类型的变量,然后打印它的值。
Sub testint()
Dim d As Integer
Debug.Print d
End Sub
回答by Swiftslide
As andy holaday said in the comment below the question, the default value of a date is simply 0. Silly me :/
正如安迪·霍拉迪在问题下方的评论中所说,日期的默认值只是 0。傻我:/