Variables || Excel VBA Master Class || 3.1

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.

Practical Introduction

' Without Variables
Sub VariablesTheory()
    If ThisWorkbook.Sheets("HomeLoan").Range("E6").Value > 500 Then
        Debug.Print ThisWorkbook.Sheets("HomeLoan").Range("C6").Value & " has been approved a home loan."
    End If
End Sub

'With Variables
Const CreditApprovalLimit As Integer = 500
Sub VariablesTheory()
    Dim CustomerName As String, CreditScore As Integer
    Dim PreferredName As String
    Dim wsHomeLoan As Worksheet
    Set wsHomeLoan = ThisWorkbook.Sheets("HomeLoan")

    CustomerName = wsHomeLoan.Range("C6").Value
    CreditScore = wsHomeLoan.Range("E6").Value
    PreferredName = wsHomeLoan.Range("C7").Value
    
    If PreferredName <> "" Then
        CustomerName = PreferredName
    End If

    If CreditScore > CreditApprovalLimit Then
        Debug.Print CustomerName & " has been approved a home loan."
    End If

End Sub

Creating Variables

Sub Variables_Theory()
    
    Dim productName As String
    productName = "Chocolate Muffin"
    Debug.Print productName
    
End Sub

Property 1: Name

Property 2: Type

Sub DetermineRows()
    Dim lastRow As ?
    lastRow = wsSales.Range("A1").CurrentRegion.Rows.Count
    Debug.Print lastRow
End Sub

Property 3: Value

Property 4: Scope

Private lastRow As Long

Sub FindLastRow()
    lastRow = wsSales.Range("A1").CurrentRegion.Rows.Count
End Sub

Sub PrintLastRow()
    Debug.Print lastRow
End Sub