VBA Programming Logic & Loops PDF
VBA Programming Logic & Loops PDF
Kipp Martin
January 4, 2012
1
Excel Files
I programminglogic.xlsm
2
Outline
Select, Case
Looping
For Loops
For Each Loops
Do While Loops
3
Motivation
4
If, Then Logic
If logical_condition Then
code to execute
End If
Dim x As Double
x = InputBox("Enter Number")
If x > 0 Then
MsgBox "Natural Logarithm = " & Log( x)
End If
If, Then Logic
Operator Description
= Equal To
<> Not equal to
> Strictly greater than
< Strictly less than
>= Greater than or equal to
<= Less than or equal to
If, Then Logic, Logical Operators
Operator Description
And All conditions must be true
Or At least one condition must be true
Not negate a condition
If x > 0 Then y = 5
instead of
If x > 0 Then
y = 5
End If
8
If, Then, Else Logic
Another logical structure is
If conditions Then
statements if true
Else
statements if false
End If
Dim x As Double
x = InputBox("Enter Number")
If x > 0 Then
MsgBox "Natural Logarithm = " & Log( x)
Else
MsgBox " The log function requires a positive number"
End If
If, Then, Else Logic
Yet, another logical structure is
Sub CaseExample1()
Dim testNum As Integer
testNum = InputBox("Enter Case from 1 to 3")
Select Case testNum
Case 1
MsgBox "Case 1 Logic"
Case 2
MsgBox "Case 2 Logic"
Case 3
MsgBox "Case 3 Logic"
Case Else
MsgBox "Number not valid"
End Select
End Sub
Select, Case (Example 2)
x = InputBox("Enter Number")
Select Case x
Case 1 To 50
discount_rate = 0.05
MsgBox "Discount Rate = " & discount_rate
Case 51 To 100
discount_rate = 0.06
MsgBox "Discount Rate = " & discount_rate
Case 101 To 200
discount_rate = 0.07
MsgBox "Discount Rate = " & discount_rate
Case Is > 200
discount_rate = 0.08
MsgBox "Discount Rate = " & discount_rate
Case Else
MsgBox "Number not valid"
End Select
Select, Case (Example 3)
The first Case that is passed will be selected.
Select Case x
Case Is <= 50
discount_rate = 0.05
MsgBox "Discount Rate = " & discount_rate
Case Is <= 100
discount_rate = 0.06
MsgBox "Discount Rate = " & discount_rate
Case Is <= 200
discount_rate = 0.07
MsgBox "Discount Rate = " & discount_rate
Case Is > 200
discount_rate = 0.08
MsgBox "Discount Rate = " & discount_rate
Case Else
MsgBox "Number not valid"
End Select
Select, Case (Example 3)
16
Looping
It is often desirable to “loop” or “iterate” over a range, an array, or
variable values. We will study:
I For Loops
I Do Loops
18
For Loops
The components of the For loop
I For VBA keyword to start loop – required
I counter – gets incremented at each step – required
I start – initial value of counter – required
I To – VBA keyword – required
I end – final value of counter – required
I Step – VBA keyword – optional
I step – the amount by which the counter is incremented (1 by
default) – optional
I Exit For – VBA keywords – optional
I Next – VBA keyword – required
I counter – optional but use it close loop
For Loops
See programminglogic.xls
Sub ForEx1()
’Illustrate the For loop
Dim i As Integer
Dim total As Double
total = 0
For i = 1 To 20
total = total + i ^ 2
Next i
MsgBox "Total = " & total
total = 0
For i = 1 To 20 Step 2
total = total + i ^ 2
Debug.Print i
Next i
End Sub
For Loops
Important Concepts:
21
For Loops
I You can nest loops
I Get values from a named range
24
For Each Loops
Sub ForEach()
’Illustrate the For Each construct
Dim ws As Worksheet, isThere As Boolean
isThere = False
Dim rng As Range
Dim cell As Range
Dim lastName As Range
Set rng = Range("names")
Set lastName = Range(rng.Cells(2, 2), _
rng.End(xlDown).Cells(1, 2))
For Each cell In lastName
If cell.Value = "Uehling" Then
isThere = True
Exit For
End If
Next cell
End Sub
Do While (Until) Loops
The construct is
26
Do While (Until) Loops
Sub DoWhile1()
’Illustrate the DoWhile construct
’Also illustrate another way to loop
’over a range without knowing its size
Dim ws As Worksheet
Set ws = Worksheets("Looping")
ws.Activate
Dim total As Double, i As Integer
total = 0
i = 1
Dim rng As Range
Set rng = Range("A23")
Do While IsEmpty(rng.Cells(i)) = False
total = total + rng.Cells(i).Value
i = i + 1
Loop
End Sub
Do While (Until) Loops
You can take:
Be careful!
Do While (Until) Loops