You are on page 1of 17

Introduction to

Visual Basic for


Application (VBA)
- Section 1
</>
What is VBA?

</>
:\> _ 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

1 Excel Options > Customize Ribbon > Activate « Developer » tab

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

5 Visual Basic Editor > View > Add Properties Window

6 Visual Basic Editor > View > Add Immediate Window

< >
6
Workbook Overview

</>
:\> _ Workbook Overview

Book1

1 A Workbook contains one or several


Col A (1) Col B (2)
Worksheets
Row 1 Cell A1 Cell B1

Row 2 Cell A2 Cell B2


… Selection is the same as:
• Range("A2:B2")
• Range(Cells(2,1), Cells(2,2)) 2 A Worksheet contains several Rows
… … and Columns

Row 10 Cell A10 Cell B10 … Selection is the same as:


• Cells(10,2) 3 A Cell is the intersection of one
Selection is the same as: • Range("B10")
• Range("A1:A10") Row and one Column
• Range(Cells(1,1), Cells(10,1))

Sheet1
4 A Range is made of one or several
Selection is the same as:
• Sheets("Sheet1") Cells
• Worksheets("Sheet1")

Selection is the same as:


• Workbooks("Book1")
< >
8
:\> _ Workbook, Worksheet, Range and Cell selections

General Use
<Worksheet_Object>.<Range_Property> = Select a particular Range in a particular Sheet

<Worksheet_Object>.<Cell_Property> = Select a particular Cell in a particular Sheet

‘Select A1 in Sheet1 VBA


Sheets("Sheet1").Range("A1").Select
Sheets("Sheet1").Cells(1, 1).Select
‘Select A1 to B10 in Sheet2
Sheets("Sheet2").Range("A1:B10").Value = 10
‘Set value of Cell A1 in Sheet1 equal to a specific variable
myVariable = "Anything you want"
Sheets("Sheet1").Range("A1").Value = myVariable
‘Set value of Cell A1 in Sheet1 equal to value of Cell B1 in Sheet2
Sheets("Sheet1").Range("A1").Value = Sheets("Sheet2").Range("B1").Value
‘Set value of Range A1:A10 in Sheet2 equal to value of Range A1:A10 in Sheet1
Sheets("Sheet2").Range("A1:A10").Value = Sheets("Sheet1").Range("B1:B10").Value

< >
9
Loops

</>
:\> _ Iterations
Objective
Run some code several times

1 General Use 2 General Use 3 General Use


For <variable> = <n1> To <n2> Do While <condition=TRUE> For each <variable> in <range>
‘ Your code to be executed several times ‘ Your code to be executed several times ‘ Your code to be executed several times
Next Loop Next

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

VBA VBA VBA


i=1
For i = 1 To 10 Do While i < 11 For each cell in Range("A1:A10")
‘ This code will run 10 times ‘ This code will run 10 times ‘ This code will run 10 times
Next i=i+1 Next

< >
Loop
11
Conditions

</>
:\> _ Conditions: IF … ELSE …
Objective
Run some code based on one or several conditions

1 General Use

If <condition_1=TRUE> Then If <condition_1=TRUE> Then If <condition_1=TRUE> Then


‘ Condition 1 is TRUE ‘ Condition 1 is TRUE ‘ Condition 1 is TRUE
Else ElseIf <cond_2=TRUE> And/Or <cond_3=TRUE> Then End If
‘ Condition 1 is FALSE ‘ Condition 2 and/or 3 are TRUE, but Condition 1 is FALSE
If <condition_2=TRUE> Then
End If Else ‘ Condition 2 is TRUE
‘ All Conditions are FALSE
End If
End If

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

INTRODUCTION TO DATASET DATA STORAGE ACCESS & EXCEL


FINAL CASE
VBA & EXCEL VBA & ACCESSIBILITY INTERACTION

1 Data > Dataset Overview

2 Public & Private > Subs, Variables & Functions

3 Excel > Data Transfer

4 Refactoring > Process Optimization


</>
Follow Me On

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. </>

You might also like