Visit my Youtube Channel
Working with Booleans || Excel VBA Master Class || 3.4c
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.
Basics
True/ False vs Numbers & Strings
Sub BooleanTheory()
Dim num1 As Long, num2 As Long
num1 = 1
num2 = 0
Debug.Print CBool(num2)
End Sub
Sub BooleanTheory()
Dim bState As Boolean
bState = True
Debug.Print CLng(bState)
End Sub
Sub BooleanTheory()
Dim num1 As Long, num2 As Long
num1 = 1
num2 = 0
If Not (num1 = num2) Then
Debug.Print "Both numbers are not equal"
End If
End Sub
'Validate Details
Dim bMissingInput As Boolean
If custName = "" Or custEmail = "" Then bMissingInput = True
If bMissingInput = True Then
MsgBox "Please enter all input values"
Exit Sub
End If
Importance Of Boolean
Sub FormSubmission()
Dim custName As Variant, custEmail As Variant
'Grab Details
custName = ThisWorkbook.Sheets("Input").Range("B4").Value
custEmail = ThisWorkbook.Sheets("Input").Range("B6").Value
'Validate Details
If IsDataMissing(custName, custEmail) = True Then
MsgBox "Please enter all input values"
Exit Sub
End If
'Paste Details
Dim lrow As Long
lrow = ThisWorkbook.Sheets("Output").Range("A1").CurrentRegion.Rows.Count + 1
ThisWorkbook.Sheets("Output").Range("A" & lrow).Value = custName
ThisWorkbook.Sheets("Output").Range("B" & lrow).Value = custEmail
'Display Confirmation Message
MsgBox "Thank you for submitting your details."
End Sub
Function IsDataMissing(fCustName As Variant, fCustEmail As Variant) As Boolean
If IsEmpty(fCustName) Or IsEmpty(fCustEmail) Then
IsDataMissing = True
Else
IsDataMissing = False
End If
End Function