Intro to Data Types || Excel VBA Master Class || 3.4a

Note: This webpage is designed to accompany the Youtube video posted above. The video offers detailed explanation on all topics; while this webpage will serve as a repository for the code presented in the video. Source code will be presented in the order of the sections within the video.

Overview of Data Types

Data Types, Values, Literals, Variables

Sub ValueTheory()
    Dim myName As String
    myName = "John Doe"
End Sub

Sub ValueTheory01()
    Dim myName As String
    myName = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
End Sub

Sub ValueTheory02()
    Dim firstName As String, lastName As String, myName As String
    firstName = "John"
    lastName = "Doe"
    myName = firstName & " " & lastName
End Sub

Sub ValueTheory03()
    Dim myName As String
    myName = GetName()
End Sub
Function GetName() As String
    GetName = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
End Function

Checking Types

Sub CheckingTypes()
    Dim i As Long
    Debug.Print TypeName(i)
End Sub

Sub CheckingTypes()
    Dim productId As Variant
    productId = ThisWorkbook.Sheets("Sheet1").Range("A1").Value
    If VarType(productId) = vbEmpty Then
        Debug.Print "No Product Id present"
    End If
End Sub

Sub RunTest()
    Call ClearCells(Sheet1.Range("A1:A3"))
End Sub

Sub RunTest()
    Call ClearCells(Sheet1)
End Sub

Sub ClearCells(area As Object)
    If TypeOf area Is Range Then
        Dim rng As Range
        Set rng = area
        rng.ClearContents
    End If
    
    If TypeOf area Is Worksheet Then
        Dim sh As Worksheet
        Set sh = area
        sh.Cells.ClearContents
    End If
End Sub

Type Conversion

Sub ImplicitConversions()

    Dim i As Integer
    Dim b As Byte
    
    b = 200
    i = b
   
End Sub

Sub ImplicitConversions()

    Dim d As Double
    Dim b As Byte
    
    d = 22.2
    b = d
    
    Debug.Print b

End Sub

Sub ExplicitConversions()

    Dim i As Long
    Dim str As String
    
    i = 1
    str = "100"
    
    Debug.Print CLng(str) + i
   
End Sub

Sub ExplicitConversions()

    Dim num1 As Long, num2 As Long
    
    num1 = 10
    num2 = 3
    
    Debug.Print CLng(num1 / num2)

End Sub

Sub ExplicitConversions()

    Dim i As Long
    Dim str As String
    
    i = 1
    str = "100"
    
    Debug.Print str + CStr(i)

End Sub

Sub ExplicitConversions()

    Dim strDate As String
    
    strDate = "31 Jan 2025"

    Debug.Print CDate(strDate)
    
End Sub

Default Values

Sub DefaultValues_Numerical()
    
    Dim i As Long
    If i = 0 Then
        Debug.Print "The Variable i still has it's default value."
    End If
    
End Sub

Sub DefaultValues_Boolean()
    
    Dim bool As Boolean
    If bool = False Then
        Debug.Print "The Variable bool still has it's default value."
    End If
   
End Sub

Sub DefaultValues_String()
    
    Dim str As String
    If str = "" Then
        Debug.Print "The Variable str still has it's default value."
    End If
   
End Sub

Sub DefaultValues_Empty()
    
    Dim productId As Variant
    productId = Sheet1.Range("A1").Value
    
    If IsEmpty(productId) = True Then
        Debug.Print "No productId present"
    End If
   
End Sub

Sub DefaultValues_Empty()
    
    Dim productId As Variant
    productId = Sheet1.Range("A1").Value
    
    Debug.Print productId
    
    productId = Empty

    If IsEmpty(productId) = True Then
        Debug.Print "No productId present"
    End If
   
End Sub

Sub DefaultValues_Empty()
    
    Dim productId As Variant
    productId = ""
    
    Debug.Print TypeName(productId)
    
    productId = Empty
    
    Debug.Print TypeName(productId)

End Sub

Sub DefaultValues_Nothing()
    
    Dim rng As Range
    If rng Is Nothing Then
        Debug.Print "The rng variable has a value of Nothing."
    End If

End Sub

Sub NULL_Value()
    
    Dim rangeFont As Variant
    rangeFont = Sheet1.Range("A1:A3").Font.Name
    Debug.Print IsNull(rangeFont)

End Sub