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

时间:2022-04-12 22:49:38

I have a function in VBA that usually returns an array. If there is nothing to return it returns an empty variable (b = empty). I have some code to loop though the array but I get an error if the variable is not an array. How can I make an if statement that doesn't cause its own error. I have tried

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

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

but this gives an error when b is an array Similarly I get error for b = null, b = nothing, b(1,1) = "" etc.

但是当b是一个数组时,这会给出一个错误。同样地,我得到b = null,b = nothing,b(1,1)=“”等的错误。

What is the best way to check this?

检查这个的最佳方法是什么?

1 个解决方案

#1


6  

To test if a variable is empty, use the IsEmpty function.

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

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

To test if a variable is an array, use the VarType function.

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

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

#1


6  

To test if a variable is empty, use the IsEmpty function.

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

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

To test if a variable is an array, use the VarType function.

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

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