Data type Variant is represent all data type for variables even more widen or general. If we declare variables without data type at the end then system consider as variant. We can choose either Dim VrtVar as Variant or Dim VrtVar only. In case we don't know what data type to use then Variant is the only choice we have.
For more example refer below table and code.
Below example we try to group the data either number, string, date or empty.
VBA Vode:
Option Explicit
Sub Examples_DataType_Variant()
Dim i As Integer, StrVar
With ActiveSheet
i = 2
Do
StrVar = .Range("B" & i)
If IsEmpty(StrVar) Then
Range("C" & i) = "Empty"
ElseIf IsDate(StrVar) Then
Range("C" & i) = "Date"
Else
If IsNumeric(StrVar) Then
Range("C" & i) = "Number"
Else
Range("C" & i) = "String"
End If
End If
Set StrVar = Nothing
i = i + 1
Loop While .Range("A" & i) <> ""
End With
End Sub
Sub Examples_DataType_Variant()
Dim i As Integer, StrVar
With ActiveSheet
i = 2
Do
StrVar = .Range("B" & i)
If IsEmpty(StrVar) Then
Range("C" & i) = "Empty"
ElseIf IsDate(StrVar) Then
Range("C" & i) = "Date"
Else
If IsNumeric(StrVar) Then
Range("C" & i) = "Number"
Else
Range("C" & i) = "String"
End If
End If
Set StrVar = Nothing
i = i + 1
Loop While .Range("A" & i) <> ""
End With
End Sub
Note:
Please remember Variant data type is not easy to handle because we need to test before proceed otherwise it will generate error.
Other Reference-Variant-data-type
Practice makes perfect. Thank You.
excelmacros
macro excel
excel programming
excel vba
No comments:
Post a Comment