Professional Documents
Culture Documents
To Visual Basic For Application (VBA) : - Section 1
To Visual Basic For Application (VBA) : - Section 1
</>
:\> _ Visual Basic for Applications (VBA)
Objective
Programing language used to automate processes
< >
3
Visual Basic Editor
</>
:\> _ Visual Basic Editor
Objective
Microsoft Office Application used to write and execute VBA code
5 Menu Bar
6 Tool Bar
3 Code Window
1 Project Explorer
Shortcut
4 Immediate Window Open VBA Editor
2 Properties ALT F11
Window
< >
5
Before we begin
2 Developer Tab > Macro Security > Trust Center > Macro Settings > Disable all macros with notification
3 Visual Basic Editor > Tools > Options > Editor > Customize Editor Format
4 Visual Basic Editor > Tools > Options > Editor > Uncheck Auto Syntax
< >
6
Workbook Overview
</>
:\> _ Workbook Overview
Book1
Sheet1
4 A Range is made of one or several
Selection is the same as:
• Sheets("Sheet1") Cells
• Worksheets("Sheet1")
General Use
<Worksheet_Object>.<Range_Property> = Select a particular Range in a particular Sheet
< >
9
Loops
</>
:\> _ Iterations
Objective
Run some code several times
Easy definition of start and end Usable with condition Cover all cells of range
Usable with Steps (2 by 2, etc.) Infinite loop possible Direct cell control
Use when start and end known Use when condition needed Use when range is covered
< >
Loop
11
Conditions
</>
:\> _ Conditions: IF … ELSE …
Objective
Run some code based on one or several conditions
1 General Use
VBA
If 1 + 1 = 1 Then Easy to read and understand
‘ FALSE: This code will not run
ElseIf 1 + 1 = 2 And 99 < 100 Then
‘ TRUE: This code will run Easy to handle multiple expressions that needs to be tested
Else
‘ FALSE: This code will not run
Use when condition is not on a single value
< >
Next
13
:\> _ Conditions: SELECT CASE
Objective
Run some code based on one or several conditions
2 General Use
Select Case <variable> Select Case <variable> Select Case <variable>
Case <value_1> Case <value_1> To <value_2> Case Is < <value_1>
‘ Variable equals value_1 ‘ Variable is within above range ‘ Variable is lower than value
Case <value_2>, <value_3> Case <value_2> To <value_3> Case Is > <value_2>
‘ Variable equals value_2 or value_3> ‘ Variable is within above range ‘ Variable is higher than value
Case Else Case Else Case Else
‘ Variable equals something else ‘ Variable equals something else ‘ Variable equals something else
End Select End Select End Select
VBA
myVar = 5 Easy to create range conditions for numerical values
Select Case myVar
Case Is < 0
‘ FALSE: This code will not run
Easier to read under certain circumstances
Case Is > 0
‘ TRUE: This code will run
Prefer when conditions is on a single numerical variable
End Select
< >
14
Debugger
</>
What’s Next?
Section 1 Section 2 Section 3 Section 4 Section 5
linkedin.com/in/benjamintermonia/
fiverr.com/btermonia
Disclaimer
All Trademarks referred to are the property of their respective owners.
This course is an independent course and is neither affiliated with, nor authorized,
sponsored, or approved by, Microsoft Corporation. </>