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
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
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
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:
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