Excel VBA Lecture 0
Excel VBA Lecture 0
Introduction
Introducing Visual Basic for Applications
• Visual Basic for Applications or VBA is a development environment
built into the Microsoft Office Suite of products
• It's a programming language that enables you to control just about
everything in Excel/Office 365/2019 integrated applications.
• VBA is an Object Oriented Programming (OOP) language.
• It works by manipulating objects. In Excel worksheets, charts and
dialog boxes are also objects.
• Macros that can be run from a button on a spreadsheet, the Excel
Ribbon, etc.
• Learning Excel VBA will enable you to do a lot more with the software
than you can via the normal spreadsheet view.
Introducing Visual Basic for Applications
In VBA the object is written first
When working in VBA tell Excel exactly what to do. Don’t assume
anything
Introducing Visual Basic for Applications
• Write your code in lower case letters. If the spelling is RIGHT, the
Visual Basic Editor will capitalize the necessary letters. If it doesn't....
check your spelling
• All VBA sentences must be on a single line. When you need to write
long sentences of code and you want to force a line break to make it
easier to read you must add a space and an underscore at the end of
each line and then press Return.
Here is an example of a single sentence broken into 3 lines:
Range("C1:C9").Sort Key1:=Range("C2"), Order1:=xlAscending, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortTextAsNumbers, Header:=xlYes
Introducing Visual Basic for Applications
Flickering Screen
Running a macro or VBA code may cause the screen to flicker as the
monitor is the slowest part of the program and cannot keep up with the
very fast changes taking place. To switch off the screen until the program
is run enter the following code line:
Application.ScreenUpdating = False
Screen comes on automatically on completion of the program
CutCopyMode
After each Paste operation, you should turn off copying:
ActiveSheet.Paste
Application.CutCopyMode = False
Introducing Visual Basic for Applications
DisplayAlerts
If you don't want Excel to ask you things like "Do you want to delete this
file..." you can use the following line of code at the beginning of the
relevant VBA procedure.
Application.DisplayAlerts = False
Then at the end make sure you use the following code to reactivate
Display Alerts.
Application.DisplayAlerts = True
Introducing Visual Basic for Applications
Compare Text
If you try to compare two strings in VBA, the system compares the Binary
information of the strings so that “My Name” Is Not Equal To “my name”.
To make the computer compare the words in the string, rather than the Binary
you need to enter the code: Option Compare Text In the Declarations area of the
module.
Quit
When you record a macro, the recorded instructions are inserted into a
Procedure whose beginning and end are denoted with the key words
Sub and End Sub. This is stored within a Module. A module can contain
many procedures.
Code generated when a macro is recorded can be modified to provide a
more customized function. To do this:
• Developer Ribbon > Code Section > Macros
• Select the desired macro from the Macro Name list
• Click Edit
The Visual Basic Editor appears.
Introducing Visual Basic for Applications
Project
Explorer
Code
Window
Properties
Window
Introducing Visual Basic for Applications
Absolute or Relative
During macro recording cell references can be made either relative to
the start position or with an absolute address.
By default, recorded macros use absolute cell referencing.
Understanding the difference:
When you use absolute references, selecting or moving from one cell to
another generates code that refers to that specific cell range.
Dim r As Range
reDim
reDim is used to change the size of one or more dimensions of an array that has already been declared
Set r = Range("A1")
Introducing Visual Basic for Applications
Arithmetic Operators
Operator Description Example
+ Adds the two operands A + B will give 15
Checks if the value of the left operand is less than the value of
< (A < B) is True.
the right operand. If yes, then the condition is true.
MsgBox "Integer is " & MyInteger & Chr(10) & "myDate is "
& myDate & Chr(10) & "myDay is " & myDay
End Sub