如果 x 是 VBA 语言中的整数,我如何表达术语?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1992922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:12:12  来源:igfitidea点击:

how do I express the term if x is integer in VBA language?

vbavb6

提问by excel34

how do I express the term if x is integer in VBA language ? I want to write a code that does something if x is integer and does something else if its not with vba excel.

如果 x 是 VBA 语言中的整数,我如何表达术语?我想编写一个代码,如果 x 是整数,那么它会做一些事情,如果它不是 vba excel,就会做其他事情。

Sub dim()
  Dim x is Variant

  'if x is integer Then 

  'Else:

End Sub 

回答by shahkalpesh

If IsNumeric(x) Then 'it will check if x is a number

If you want to check the type, you could use

如果你想检查类型,你可以使用

If TypeName(x) = "Integer" Then

回答by Fionnuala

This may suit:

这可能适合:

If x = Int(x) Then

回答by Oorang

It depends on if you mean the data type "Integer", or Integer in the sense of: "A number with no decimal." If you meant the latter, then a quick manual test will suffice (see first example); if you meant the former then there are three ways to look at data types all with various pros and cons:

这取决于您的意思是数据类型是“整数”,还是意义上的整数:“没有小数的数字”。如果您指的是后者,那么快速手动测试就足够了(参见第一个示例);如果您指的是前者,那么可以通过三种方法来查看数据类型,所有这些都各有利弊:

  1. VarType will tell you the variant's sub type (see example). It's reasonably fast as it's a just a memory read to an enum, but can only be used on variants and won't tell you the specific object type. Additionally the variant's sub type is often assigned automatically (generally tending to type with the smallest suitable datatype). But it can be tricky (see example).
  2. TypeName is the most flexible and robust. It can tell you specific class types and Variant sub-types. It has a slight drawback in that to test requires a string comparison, so there is a minuscule performance hit. It won't be noticeable unless there is some serious repetition though. Also if you have two Objects of the same name in your project (Example Word.Range and Excel.Range), TypeName can't tell the difference (it will return "Range" for both).
  3. Finally there is the TypeOf operator (which won't help you here). The TypeOf operator can not test for primitives (example: Integer, Long, String, etc.) but it can tell the specific type of object (example. Excel.Range vs Word.Range). The TypeOf operator will throw an error if the object is nothing, so you must always pretest the object for "Not Is Nothing" but being an operator rather than a function, it is much faster than TypeName (even with the pretest).
  1. VarType 会告诉您变体的子类型(参见示例)。它相当快,因为​​它只是对枚举的内存读取,但只能用于变体并且不会告诉您特定的对象类型。此外,变体的子类型通常是自动分配的(通常倾向于使用最小的合适数据类型进行输入)。但这可能很棘手(参见示例)。
  2. TypeName 是最灵活和健壮的。它可以告诉您特定的类类型和 Variant 子类型。它有一个小缺点,即测试需要字符串比较,因此对性能的影响很小。除非有一些严重的重复,否则它不会引起注意。此外,如果您的项目中有两个同名的对象(例如 Word.Range 和 Excel.Range),则 TypeName 无法区分(它将为两者返回“Range”)。
  3. 最后是 TypeOf 运算符(在这里对您没有帮助)。TypeOf 运算符无法测试基元(例如:Integer、Long、String 等),但可以判断对象的特定类型(例如 Excel.Range 与 Word.Range)。如果对象什么都不是,TypeOf 运算符将抛出错误,因此您必须始终针对“Not Is Nothing”对对象进行预测试,但作为运算符而不是函数,它比 TypeName 快得多(即使使用预测试)。
Public Sub ExampleManual()
    Dim d As Double
    d = 1
    If Fix(d) = d Then
        MsgBox "Integer"
    End If
End Sub

Public Sub ExampleTypeName()
    Dim x As Integer
    MsgBox TypeName(x)
End Sub

Public Sub ExampleTypeOf()
    Dim x As Excel.Range
    Set x = Selection
    ''//Using TypeOf on Objects set to Nothing will throw an error.
    If Not x Is Nothing Then
        If TypeOf x Is Excel.Range Then
            MsgBox "Range"
        End If
    End If
End Sub

Public Sub ExampleVarType()
    Dim x As Variant
    ''//These are all different types:
    x = "1"
    x = 1
    x = 1&
    x = 1#
    Select Case VarType(x)
        Case vbEmpty
        Case vbNull
        Case vbInteger
        Case vbLong
        Case vbSingle
        Case vbDouble
        Case vbCurrency
        Case vbDate
        Case vbString
        Case vbObject
        Case vbError
        Case vbBoolean
        Case vbVariant
        Case vbDataObject
        Case vbDecimal
        Case vbByte
        Case vbUserDefinedType
        Case vbArray
        Case Else
    End Select
End Sub