如果在VBA语言中x是整数,我该如何表达这个术语?我想编写一个代码,如果x是整数,它会执行某些操作,如果不是vba excel则执行其他操作.
Sub dim()
Dim x is Variant
'if x is integer Then
'Else:
End Sub
Run Code Online (Sandbox Code Playgroud)
sha*_*esh 21
If IsNumeric(x) Then 'it will check if x is a number
Run Code Online (Sandbox Code Playgroud)
如果要检查类型,可以使用
If TypeName(x) = "Integer" Then
Run Code Online (Sandbox Code Playgroud)
Oor*_*ang 12
这取决于你是指数据类型"Integer",还是意思是:"没有小数的数字".如果你的意思是后者,那么快速手动测试就足够了(见第一个例子); 如果你的意思是前者,那么有三种方法来查看数据类型,它们各有利弊:
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
Run Code Online (Sandbox Code Playgroud)