Excel VBA中的TypeName返回什么?

时间:2016-07-14 04:42:27

标签: excel vba excel-vba types

我想查看数据类型是什么:Cells(2, 1).Value即“这里有什么用途?”下面。

我尝试了名字(Integer / Long等),但似乎不接受。

If TypeName(Cells(r, 1).Value) = "WHAT GOES HERE?"  Then
    MsgBox "Yes"

Else
    MsgBox "No"

End If

4 个答案:

答案 0 :(得分:9)

好好查看MSDN会显示一个可能的返回值表

String Returned     Variable
-------------------------------------------------------------------
Object type         An object whose type is objecttype
Byte                Byte value
Integer             Integer
Long                Long integer
Single              Single-precision floating-point number
Double              Double-precision floating-point number
Currency            Currency value
Decimal             Decimal value
Date                Date value
String              String
Boolean             Boolean value
Error               An error value
Empty               Uninitialized
Null                No valid data
Object              An object
Unknown             An object whose type is unknown
Nothing             Object variable that doesn't refer to an object

此外,您还可以使用内置帮助查看上表(功能归功于Axel Richter)。要快速跳转到相应页面,请选择该功能并按 F1 或通过对象浏览器导航到帮助页面,如下所示:

enter image description here

答案 1 :(得分:3)

如果您的单元格包含数字,则可能的TypeName为" Double"

If TypeName(Cells(r, 1).Value) = "Double"  Then
    MsgBox "Yes"

Else
    MsgBox "No"

End If

答案 2 :(得分:1)

快速说明:你可以尝试一下。使用值填写要检查的单元格并使用

MsgBox TypeName(cells(2, 1).Value)

如果单元格为空,则返回Empty

答案 3 :(得分:0)

如果您需要MSDN Table中的变量编号,则需要VarType。 像这样:

+===================+=======+====================================================+ | Constant | Value | Description | +===================+=======+====================================================+ | vbEmpty | 0 | Empty (uninitialized) | +-------------------+-------+----------------------------------------------------+ | vbNull | 1 | Null (no valid data) | +-------------------+-------+----------------------------------------------------+ | vbInteger | 2 | Integer | +-------------------+-------+----------------------------------------------------+ | vbLong | 3 | Long integer | +-------------------+-------+----------------------------------------------------+ | vbSingle | 4 | Single-precision floating-point number | +-------------------+-------+----------------------------------------------------+ | vbDouble | 5 | Double-precision floating-point number | +-------------------+-------+----------------------------------------------------+ | vbCurrency | 6 | Currency value | +-------------------+-------+----------------------------------------------------+ | vbDate | 7 | Date value | +-------------------+-------+----------------------------------------------------+ | vbString | 8 | String | +-------------------+-------+----------------------------------------------------+ | vbObject | 9 | Object | +-------------------+-------+----------------------------------------------------+ | vbError | 10 | Error value | +-------------------+-------+----------------------------------------------------+ | vbBoolean | 11 | Boolean value | +-------------------+-------+----------------------------------------------------+ | vbVariant | 12 | Variant (used only with arrays of variants) | +-------------------+-------+----------------------------------------------------+ | vbDataObject | 13 | A data access object | +-------------------+-------+----------------------------------------------------+ | vbDecimal | 14 | Decimal value | +-------------------+-------+----------------------------------------------------+ | vbByte | 17 | Byte value | +-------------------+-------+----------------------------------------------------+ | vbLongLong | 20 | LongLong integer (Valid on 64-bit platforms only.) | +-------------------+-------+----------------------------------------------------+ | vbUserDefinedType | 36 | Variants that contain user-defined types | +-------------------+-------+----------------------------------------------------+ | vbArray | 8192 | Array | +-------------------+-------+----------------------------------------------------+

然后你可以简单地使用

If VarType(TempArray) = vbObject Then 要么 If VarType(TempArray) = 9 Then