根据变量的类型做一些事情

时间:2012-10-23 09:54:34

标签: vba excel-vba excel

我在VBA中有一个通常返回数组的函数。如果没有要返回的内容,则返回一个空变量(b = empty)。 我有一些代码循环通过数组但如果变量不是数组我得到一个错误。如何创建不会导致自身错误的if语句。我试过了

if not b = empty then
'do the loop
end if

但是当b是数组时会出错 同样地,我收到了b = nullb = nothingb(1,1) = ""等错误。

检查此问题的最佳方式是什么?

1 个答案:

答案 0 :(得分:6)

要测试变量是否为空,请使用IsEmpty函数。

If IsEmpty(b) Then
    Debug.Print "b is Empty"
End If

要测试变量是否为数组,请使用VarType函数。

If VarType(b) And vbArray Then
    Debug.Print "b is an array"
End If
相关问题