Excel 2016 VBA PPT Slide Deck
Excel 2016 VBA PPT Slide Deck
Excel 2016 VBA PPT Slide Deck
Excel
CHELSEA DOHEMANN
Today We’re Going to Day 1
Cover
Morning Afternoon
Introduction to Macros VB Grammar
Recording Macros Inserting & Formatting Text
Editing Macros Sorting Data
VBA Interface Variables & Operators
Macro Security Duplicating Data
Debugging Generating Reports
Customizing Ribbon and Toolbars Various Properties, Objects and
Methods
Today We’re Going to Day 1
Cover
Morning Afternoon
Introduction to Macros VB Grammar
Recording Macros Inserting & Formatting Text
Editing Macros Sorting Data
VBA Interface Variables & Operators
Macro Security Duplicating Data
Debugging Generating Reports
Customizing Ribbon and Toolbars Various Properties, Objects and
Methods
Object-Oriented
Programming
Color
Purpos
e
Size
Object
Project
Code
Explorer
Window
Properties
Window
Immediate Window
Modules
Objects
Columns
Cells A1
Sheets Tables Charts
Rows
Hierarchy of Objects Application
Workbooks
Worksheet
s
A1 .Color
$100
$1,000,000 .Value
Text
Text .Font
Methods
Copy
Clear
Contents
Column
Auto Fit
s
VBA Comments
“comment out” a block all at once
Macro Security Settings
Digital Certificates
Digital Signatures
The Debugging
Process Determine problem
Compile Step-Into
Intellisense Window
Method
s
Properti
es
Objects
Object Browser
The Range Object
MyRange = “C3”
Range(MyRange)
Color Options in VBA
Theme Colors VB Colors
xlThemeColorLight1 vbBlack
xlThemeColorDark1 vbWhite
xlThemeColorDark2 vbRed
xlThemeColorlight2 vbLine
xlThemeColorAccent1 vbYellow
xlThemeColorAccent2 vbBlue
xlThemeColorAccent3 vbMagenta
xlThemeColorAccent4 vbCyan
xlThemeColorAccent5 Range (“A1”) . Font . Color =
xlThemeColorAccent6 vbRed
Range (“A1”) . Font . ThemeColor = xlThemeColorLight1
Color Options in VBA
RGB Colors Hexidecimal
Blue – ( 0 , 0 , 255 ) (Hex)
Yellow – ( 255 , 255 , 0 ) Blue – 16711680
Cyan – ( 0 , 255 , 255 ) Yellow – 65535
Magenta – ( 255 , 0 , 255 Cyan – 16776960
) Magenta – 16711935
Range (“A1”) . Font . Color =
RGB(1,1,1)
(Can’t have the # sign, which
people are used to seeing with
the hex code)
Range(“A1”) . Value =
“Hello”
The ActiveSheet Object
ActiveSheet . Name =
“Sheet1”
The Name Property
ActiveSheet . Name =
“Sheet1”
Concatenation
&
The Select Method
Range(“B2”) Select
.
The CurrentRegion
Property
Range(“A1”) . CurrentRegion .
Select
Practice
Insert Headers
Format Headers, and Columns
Format Cell Interior of Lists
Variables
DATA STORAGE
Different Data Types
Numbers Text
Decimal Double Integer Variant String Char
• 28 decimal • Gazillians • -2M – 2M • All • 2B • 0 - 65535
placeholders • 8 bytes • 2 bytes • 16-32 characters • 2 bytes
• 14 bytes bytes • Depends
Boolean / Byte
Misc
Date Object Range
Boolean Byte
• 1900- • Any • Any
• True / False • 0-255
9999 • 4-32 • 4-32
• 2 bytes • 1 byte • 8 bytes bytes bytes
Variables
Sunshine? Or Clouds?
Sunshin
Clouds
e
Sunshin
e
NameofVariable
Boolean
Integer Name: ExBoolean
Name: ExInteger ExBoolean = True
Data: 5.5 If ExBoolean = True then
MsgBox “Value is” & MsgBox “Boolean variables
example value are cool!”
(Math) Operators
+ - * /
< > = <>
Loops
TO REPEAT ACTIONS MULTIPLE TIMES
Open “For Next Loop” XLSM
The For Next Loop
To make an action repetitive (a specific number of repetitions)
Scenario:
We want to input the value “100” multiple times into some cells.
Strategy:
We’ll make a variable called “x” and use it to stand in for the numerical
value of each iteration of the loop.
The For Next Loop
First: Define your variable
Dim x as Integer
The For Next Loop
Next: Write the code that we want to repeat
Dim x as Integer
Dim x as Integer
For
Range(x, 1).Value = 100
Next
The For Next Loop
Next: enter the code to establish he number of times this should repeat
Dim x as Integer
For x = 1 - 10
Range(x, 1).Value = 100
Next
The For Next Loop
Finish: complete the Next statement, tell it to move to the next x
Dim x as Integer
For x = 1 - 10
Range(x, 1).Value = 100
Next x
The For Next Loop
Challenge! Try out the two other For Loop examples in this
workbook.
Scenario:
We want to input the value “100” multiple time, until we reach Row 10.
Strategy:
We’ll make a variable called “x” and use it to stand in for the numerical
value of each iteration of the loop.
The Do While Loop
First: Define your variable
Dim x as Integer
The Do While Loop
Then: Define your starting value
Dim x as Integer
X = 1
The Do While Loop
Then: Write the code that you want to repeat
Dim x as Integer
x = 1
Dim x as Integer
x = 1
Do While
Cells(x, 1).Value = 100
Loop
The Do While Loop
Then: code the criteria for stopping the loop
Dim x as Integer
x = 1
Do While x < 10
Cells(x, 1).Value = 100
Loop
The Do While Loop
Then: code the statement to count up after you’ve executed the repeatable code
Dim x as Integer
x = 1
Do While x < 10
Cells(x, 1).Value = 100
x = x + 1
Loop
The Do While Loop
Challenge! Try out the two other For Loop examples in this
workbook.
Offset( 1 , 1 )
Offset( 1 , 1 )
Offset(1, 1)
Offset(-1, -1)
The Copy Method
Selection.Copy
The Paste Method
ActiveSheet.Paste
The Columns Property
Columns(“B:B”).Select
The AutoFit Method
Doesn’t fit! Now it fits!
Columns(“B:C”).AutoFit
The Address Property
The Call Statement
We can have a Procedure run other macros using the “Call” statement
The Font Property
ActiveCell.Font.Name = “Verdana”
The End Property
ActiveCell.End(x1Down) ActiveCell.End(x1toRight)
Message Boxes
MsgBox options
MsgBox “Message”, vbOption, “Message Box Title”
vbYesNoCancel vbRetryCancel
vbInformatio
n
Input Boxes
Name of Box
Constants
Declaring constants works a lot like declaring variables, EXCEPT the
value has to be defined at the same time.
…previous code….
Is the
YES Condition no
TRUE?
Next code…
Decision Structures
Code blocks that allow us to: Run a statement if a condition is TRUE, and another if
FALSE
…previous code….
Is the
YES Condition NO
TRUE?
Next code…
Decision Structures
Code blocks that allow us to: Run a statement if a condition is
FALSE,
…previous code….
Is the
yes Condition NO
TRUE?
Next code…
The IF THEN Structure
If B4 is greater than 60
Enter “Pass” in C4
The IF THEN ELSE
Structure
If B4 is greater than 60
Enter “Pass” in C4
If B4 is LESS than 60
Enter “Fail” in C4
If Then Else Structures can get
looong…
value Number(s)
Case “Word” Case 1, 2
Worksheets.Add
The Add Method
Worksheets.Add Before:=Worksheets(3)
The Add Method
Worksheets.Add After:=Worksheets(1)
The Copy Method
Worksheets(2).Copy Before:=Worksheets(4)
Worksheets(2).Copy After:=Worksheets(2)
The Delete Method
Worksheets(2).Delete
The DateSerial Function
Standard
Currency
Percent
The Number Format
Option
Unlike the Format Function, this just changes the formatting of the number;
Maintains the serial number for the date
The Move Method
Worksheets(2).Move Before:=Worksheets(4)
Worksheets(2).Move After:=Worksheets(2)
The PrintPreview Method
Worksheet
Worksheet
object
object reference
reference
Previews
Previews allall the
the worksheets
worksheets
in
in the
the workbook
workbook Worksheets.PrintPreview
Previews
Previews the
the second
second worksheet
worksheet Worksheets(2).PrintPreview
in
in the
the workbook
workbook
Previews
Previews the
the worksheet
worksheet Worksheets(“SE Sales”).PrintPreview
named
named SE
SE Sales
Sales
The PrintOut Method
Worksheet
Worksheet
object
object reference
reference
Prints
Prints all
all the
the worksheets
worksheets
in
in the
the workbook
workbook Worksheets.PrintOut
Prints
Prints the
the second
second worksheet
worksheet
in
in the
the workbook
workbook
Worksheets(2).PrintOut
Prints
Prints the
the worksheet
worksheet
named
named SESE Sales
Sales
Worksheets(“SE Sales”).PrintOut
User-Defined Functions
Function
Function Name
Name
Optional
Optional variables
variables
Beginning
Beginning
statement
statement Public Function Pricelevel(amount, byr, eyr)
If byr < 1913 Or byr > 2003 Or eyr < 1913 or eyr > 2003 Then
PriceLevel = 0
Processing
Processing
code
code Else Result
Result placed
placed into
into aa variable
variable having
having the
the function
function name
name
End If
Closing
Closing
statement
statement End Function
Arguments
Data
Data sent
sent from
from worksheet
worksheet
to
to function
function for
for processing
processing
Arguments
Arguments listed
listed within
within parentheses
parentheses
Declared Range Objects
Range
Range object
object name
name
The Set Statement
Formula
Formula property
property attached
attached to
to
Range
Range object
object
MyRange.Formula = “=SUM(A1:C6)”
The Columns Property
Fixed
possibilities
Currency Displays thousand separators as well as two decimal places.
Displays at least one digit to the left of the decimal place and
two digits to the right of the decimal place.
Displays the thousand separators, at least one digit to the left of
Standard the decimal place, and two digits to the right of the decimal
place.
Displays a percent value - that is, a number multiplied by 100
Percent with a percent sign. Displays two digits to the right of the
decimal place.
Scientific Scientific notation.
Displays No if the number is 0. Displays Yes if the number is not
Yes/No
0.
Displays False if the number is 0. Displays True if the number is
True/False
not 0.
Displays Off if the number is 0. Displays On is the number is not
On/Off
0.
General Date Displays date based on your system settings
Long Date Displays date based on your system's long date setting
Medium Date Displays date based on your system's medium date setting
Short Date Displays date based on your system's short date setting
Long Time Displays time based on your system's long time setting
Time for a Break