Excel中的TypeName VBA返回什么?

我想检查一下数据types是什么: Cells(2, 1).Value ie“What is GOE HERE?” 下面。

我试过名称(整数/长等),但似乎并不接受。

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

那么看看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)查看上述表格。 要快速跳转到相应页面,请select该function,然后按F1或通过对象浏览器导航到帮助页面,如下所示:

在这里输入图像说明

如果您的单元格包含一个数字,那么可能的TypeName是“Double”

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

快速提示:你可以试试看。 填写你想要检查的单元格的价值和使用

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

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

如果你需要MSDN表中的variables号,那么你需要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