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

2020-05-23 02:55发布

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.

Sub dim()
  Dim x is Variant

  'if x is integer Then 

  'Else:

End Sub 

标签: vba vb6
3条回答
放荡不羁爱自由
2楼-- · 2020-05-23 03:18

This may suit:

If x = Int(x) Then
查看更多
beautiful°
3楼-- · 2020-05-23 03:27
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
查看更多
Animai°情兽
4楼-- · 2020-05-23 03:30

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).
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
查看更多
登录 后发表回答