VBA 检查变量是否为空
我有一个对象,在其中我想检查某些属性是否设置为 False
,例如:
If (Not objresult.EOF) Then
'Some code
End if
但有时,objresult.EOF
是 Empty
;我该如何检查?
IsEmpty
函数仅适用于 Excel 单元objresult.EOF Is Nothing
- 返回Empty
objresult.EOF <> null
- 也返回Empty
!
I have an object and within it I want to check if some properties are set to False
, like:
If (Not objresult.EOF) Then
'Some code
End if
But sometimes, objresult.EOF
is Empty
; how can I check for this?
IsEmpty
function is for excel cells onlyobjresult.EOF Is Nothing
- returnsEmpty
objresult.EOF <> null
- returnsEmpty
as well!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的测试方式取决于属性的数据类型:
您可以通过按 F2 启动对象浏览器并查找对象来告知数据类型。另一种方法是仅使用 TypeName 函数:
MsgBox TypeName(obj.Property)
How you test depends on the Property's DataType:
You can tell the DataType by pressing F2 to launch the Object Browser and looking up the Object. Another way would be to just use the TypeName function:
MsgBox TypeName(obj.Property)
要检查
Variant
是否为 Null,您需要这样做:或
To check if a
Variant
is Null, you need to do it like:or
对于数字,这很棘手,因为如果数字单元格为空,VBA 会为其分配默认值 0,因此 VBA 代码很难区分输入的零和输入的数字空白数字单元格。
以下检查对我有用,以查看单元格中是否输入了实际的 0:
For a number, it is tricky because if a numeric cell is
empty
VBA will assign a default value of 0 to it, so it is hard for your VBA code to tell the difference between an entered zero and a blank numeric cell.The following check worked for me to see if there was an actual 0 entered into the cell:
我遇到了类似的问题,整数可以在 Access VBA 中合法地分配为 0。上述解决方案都不适合我。
起初我只使用了布尔变量和 IF 语句:
在我的例子中,我的整数变量赋值是在 for 循环和另一个 IF 语句中,所以我最终使用了“Exit For”,因为它是更简洁。
像这样:
I had a similar issue with an integer that could be legitimately assigned 0 in Access VBA. None of the above solutions worked for me.
At first I just used a boolean var and IF statement:
In my case, my integer variable assignment was within a for loop and another IF statement, so I ended up using "Exit For" instead as it was more concise.
Like so: