




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of control logic and loops in visual basic for applications (vba), including the use of if, select case, for, and do loops. It covers the basics of conditions, logical operators, and nested statements, as well as best practices for documenting and using these constructs.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!
If condition
Then
statement
If numberOrdered <= 200 Then unitCost = 1.30If intNumOrdered <= 200 Then dblUnitCost = 1.
If intNumOrdered <= 200 Then
dblUnitCost = 1.30MsgBox “The unit cost is ” & unitCost Else
dblUnitCost = 1.25MsgBox “The unit cost is ” & unitCost End If
If condition
Then
Statement(s) [Else
Statement(s)
End If
If intNumOrdered <= 200 Then
dblUnitCost = 1. Else
dblUnitCost = 1. End If MsgBox “The unit cost is ” & unitCost
If condition
And
condition
Then
If condition
Or
condition
Then
If Color = “Red” Or “Blue” Then If Color = “Red” Or Color = “Blue” Then
If condition
And
condition
Or
condition
Then
If Cell.Font.Bold = True And Cell.Color = “Blue” Or Cell.Color = “Red” ThenIf (
condition
And
condition2)
Or
condition
Then
If condition
And (
condition
Or
condition3)
Then
If (Cell.Font.Bold = True And Cell.Color = “Blue”) Or Cell.Color = “Red” ThenIf Cell.Font.Bold = True And (Cell.Color = “Blue” Or Cell.Color = “Red”) Then
Select Case
Variable
Case
Value Statement(s) Case
Value Statement(s) …[Case Else
Other Statement(s)
End Select
Select Case intProductIndexCase Is <= 3
dblUnitPrice = 1.20 * dblUnitCost Case 4 To 6
dblUnitPrice = 1.30 * dblUnitCost Case 7
dblUnitPrice = 1.40 * dblUnitCost Case Else
dblUnitPrice = 1.10 * dblUnitCost End Select
Select Case strProductCase Is = “Widgets”
If intNumOrdered <= 200 Then
dblUnitCost = 1. Else
dblUnitCost = 1. End If Case Is =“Gadgets”
If intNumOrdered <= 500 Then
dblUnitCost = 2. ElseIf intNumOrdered <= 600 Then
dblUnitCost = 2. Else
dblUnitCost = 2. End If Case Else
dblUnitCost = 2. End Select
If strProduct = “Widgets” Then
If intNumOrdered <= 200 Then
dblUnitCost = 1. Else
'Widgets & intNumOrdered > 200
dblUnitCost = 1. End If
ElseIf strProduct = “Gadgets” Then
If intNumOrdered <= 500 Then
dblUnitCost = 2. ElseIf intNumOrdered <= 600 Then
dblUnitCost = 2. Else
'Gadgets & intNumOrdered > 600 dblUnitCost = 2. End If Else 'strProduct <> “Gadgets” and <>“Widgets”
dblUnitCost = 2. End If 'strProduct = “Widgets”
For i =
First
To
Last
[Step
Increment
Statement(s) Next [i]
item
As
Object
For Each
item
in
Collection
Statement(s) Next
Dim ws As Worksheet, boolFound As BooleanboolFound = FalseFor Each ws in ActiveWorkbook.Worksheets
If ws.Name = “Data” Then
boolFound = TrueExit For End If Next ws“ws” is an arbitrary variable name
Dim
item
As
Object
For Each
item
in
Collection
Statement(s) Next
Dim thisCell As Range, counter As Integercounter = 0For Each thisCell in Range(“Data”)
If thisCell.HasFormua = True Then counter = counter + Next thisCellMsgBox “There are ” & counter & “cells in the Data range” _& “that contain formulas.”
Counts the number of Cells w/ a formula
Do
Loop While
Do
Loop Until
Check condition atthe bottom of theloop (statementsexecute at least once)
Do While
Loop
Do Until
Loop
Check condition atthe top of the loop(may never executestatements)
Loop
condition exists
Loop
exists
Sub FindJames()
Dim boolFound As Boolean, intRowIndex As IntegerboolFound = False
intRowIndex = 1Do while Worksheets(“Salaries”).Cells(intRowIndex,1) <> “”
If Worksheets(“Salaries”).Cells(intRowIndex,1) = “James Snyder” Then
boolFound = TrueExit Do
End If LoopIf boolFound = True Then
MsgBox “James Snyder is in the employee list.”
Else
MsgBox “James Snyder is not in the employee list.”
End If
End Sub