Excel VBA Vocabulary For Macros PDF
Excel VBA Vocabulary For Macros PDF
1/13
11/12/2014
The Visual Basic Editor will help you avoid errors in coding in many different ways. You will not have to
wait at the end to be told that there is something wrong with your macro.
Spelling Errors
You have seen in lesson 11 the VBE capitalise letters to let you know that there are no spelling errors.
Syntax Errors
The VBE will also tell you that there is a syntax error in what you have just written by making the font red
and showing you a message box.
Exercise 1
Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor (VBE).
Step 2: In the code window of any of the sheet copy/paste the following line of code:
Range(A1").Select and click "Enter".
You get the following message box telling you that you are missing a "list separator". Look for the error
before the segment highlighted in blue. We can deduce that VBA is talking about the missing quotation
mark.
2/13
11/12/2014
3/13
11/12/2014
Step 1: Open a new workbook in Excel and use the ALT/F11 keys to go to the visual basic editor (VBE).
Step 2: Copy the following macro in the code window of any sheet. As you can read: starting in cell A1
a value of "99" will be entered in the selected cell then the cursor will move one cell down to enter "99",
repeat the process until the row number of the selected cell is 3000 and come back to cell A1.
Sub testLesson13b1()
Range("A1").Select
Do Until Selection.Row = 3000
Selection.Value = 99
Selection.Offset(1, 0).Select
Loop
Range("A1").Select
End Sub
Step 3: Run the macro from Excel as you did with the previous one.
Step 4: Remove all the "99" from the cells
Step 5: Copy the following macro in the code window of a new workbook and run it. Two lines of code
have been added to the previous macro to prevent all the steps of the action to be seen on the screen.
Sub testLesson13b2()
Application.ScreenUpdating = False
Range("A1").Select
Do Until Selection.Row = 3000
Selection.Value = 99
Selection.Offset(1, 0).Select
Loop
Range("A1").Select
Application.ScreenUpdating = True
End Sub
Step 6: Run the macro from Excel as you did with the previous one. You will see a blank sheet, no
movement whatsoever and then a sheet where cells A1 to A3000 are equal to "99".
Sometimes you or the users might want to see the action. Some other times you or the user do not want
to see the action. It is up to you to use the sentence or not.
You can even use the pair of sentences (as below) anywhere within a long macro to refresh the screen
at significant points in the process. With the pair of sentences you call for a refreshment with
Application.ScreenUpdating = True and then interrupt the refreshment process until the next
refreshment with Application.ScreenUpdating = False. Before the end of the macro you will use a final
Application.ScreenUpdating = True.
The pair of refreshing sentences:
Application.ScreenUpdating = True
Application.ScreenUpdating = False
Step 7: Close the workbook without saving anything
Here is a sample of what you will find in lchapter14
of the downloadable Tutorial on Excel macros
4/13
11/12/2014
ThisWorkbook is the workbook within which your VBA procedure runs. So if you write:
ThisWorkbook.Save
The workbook within which your VBA procedure (macro) runs will be saved.
If you want to close the workbook within which your VBA procedure (macro) runs without saving it you
will write these two lines of code:
ThisWorkbook.Saved=True
ThisWorkbook.Close
Verifying the existence of a file
When you want to verify if a certain file exists on your disk you will use the following code that means "If
the file "C:\Stuff\toto.xls" does not exist then":
If Dir("C:\Stuff\toto.xls") = "" Then
You could also use a sentence that means "If the file "C:\Stuff\toto.xls" does exist then":
If Dir("C:\Stuff\toto.xls") <> "" Then
If you are looking in the same folder as the file in which the macro runs you can simplify the VBA code:
If Dir("toto.xls") <> "" Then
In the downloadable tutorial on Excel macros you will find many other uses for Dir including opening all
the files of a folder to generate a consolidated database (whatever the number of files in the folder). You
will also learn about Path, ActiveWorkbook, Windows, Kill, and many other VBA words to work with
one or many workbooks.
Here is a sample of what you will find in lchapter15
of the downloadable Tutorial on Excel macros
5/13
11/12/2014
Cells(1,1).Select means (row 1, column 1) and is the same thing as Range("A1").Select and
Cells(14,31).Select means (row 14, column 31) and is the same as Range("AE14").Select.
We strongly recommend that you use Range instead of Cells to work with cells and groups of cells. It
makes your sentences much clearer and you are not forced to remember that column AE is column 31.
The only time that you will use Cells is when you want to select all the cells of a worksheet. For
example:
Cells.Select
To select all cells and then empty all cells of values or formulas you will use:
Cells.ClearContents
Range
To select a single cell you will write:
Range("A1").Select
To select a set of contiguous cells you will use the colon and write:
Range("A1:G5").Select
To select a set of non contiguous cells you will use the comma and write:
Range("A1,A5,B4").Select
To select a set of non contiguous cells and a range you will use both the colon and the comma:
Range("A1,A5,B4:B8").Select
Offset
The Offset property is the one that you will use the most with Range to move around the sheet.
To move one cell down (from B2 to B3): Range("B2").Offset(1,0).Select
To move one cell to the right (from B2 to C2): Range("B2").Offset(0,1).Select
To move one cell up (from B2 to B1): Range("B2").Offset(-1,0).Select
To move one cell to the left (from B2 to A2): Range("B2").Offset(0,-1).Select
To move one cell down from the selected cell:
ActiveCell.Offset(1,0).Select
As you notice the first argument between the parentheses for Offset is the number of rows and the
second one is the number of columns. So to move from A1 to G6 you will need:
Range("A1").Offset(5,6).Select
You will use very often the following piece of code . It selects a cell PLUS 4 more to the right to be
copied/pasted somewhere else:
Range(ActiveCell,ActiveCell.Offset(0,4)).Copy
Notice the comma after the first ActiveCell and the double closing parentheses before the Copy.
There are many important VBA words to discover in the downloadable Tutorial on Excel Macros. You
have already read something about Range, Cells, Offset, ActiveCell, read some more about them and
about many other powerful words like CurrentRegion, UsedRange, End(xlDown), Formula, Value,
FormulaR1C1, ClearContents, Delete, and many more.
Here is a sample of what you will find in lchapter17
of the downloadable Tutorial on Excel macros
6/13
11/12/2014
Step 4: Delete the macro in the Visual Basic Editor and the value 695 from cell A1
Exercise 2
You might want to tell the user where he will find the result.
Step 1: Use the ALT/F11 keys to move to the Visual Basic Editor.
Step 2: Copy/Paste the following macro from here into the code window of any sheet.
Sub proLessson17b()
Sheets("Sheet1").Select
Range("A1").Value = 695
MsgBox "The result is in cell ""A1"""
End Sub
Notice the space following MsgBox, the use of quotation marks surrounding the text and the double
quotation mars around A1 because we want the address to show on the message box between
quotation marks.
Step 3: Use the ALT/F11 keys to go back to Excel and run the macro proLessson17b.
The value 695 is entered in cell A1 and the following message box appears
Step 4: Delete the macro in the Visual Basic Editor and the value 695 from cell A1
Exercise 3
Instead of telling the user that the value is in cell A1, you might want to tell him what the result is in the
message box itself.
Step 1: Use the ALT/F11 keys to move to the Visual Basic Editor.
Step 2: Copy/Paste the following macro from here into the code window of any sheet.
Sub proLessson17c()
Sheets("Sheet1").Select
Range("A1").Value = 695
MsgBox "The result is " & Range("A1").Value
End Sub
Notice the space following MsgBox, the use of quotation marks surrounding the text, the space at the
end of the text and the spaces surrounding the ampersand.
Step 3: Use the ALT/F11 keys to go back to Excel and run the macro proLessson17c.
The value 695 is entered in cell A1 and the following message box appears
http://www.excel-vba.com/excel-vba-solutions-intermediate.htm
7/13
11/12/2014
Number
Jones
Tom
Barry
2
3
Peter
Here is another simplified Excel macro sorting data using criteria in three different fields.
Sub proFilter()
Range("A1").Sort Key1:=Range("A2"), Order1:=xlAscending, Key2:=Range( _
"B2"), Order2:=xlAscending, Key3:=Range("C2"), Order3:=xlAscending, _
Header:=xlYes
End Sub
The code in the two procedures above is much simpler than the following recorded macro in Excel 2007
and 2010. This recorded macro will not work in earlier versions of Excel (1997 to 2006).
http://www.excel-vba.com/excel-vba-solutions-intermediate.htm
8/13
11/12/2014
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A7"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B2:B7"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C7"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:E7")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
In the downloadable tutorial on Excel macros we offer you much more vocabulary to work with Excel
databases and also many more simplified macros that can be used in all versions of Excel. You can you
can copy/paste any of them into your own workbooks.
Here is a sample of what you will find in lchapter19
of the downloadable Tutorial on Excel macros
First enter xxx where you want the loop to stop (below the last value: B7). Select the cell at the top of
the column containing the values to be considered (B1)and run the macro.
Sub proDelete()
Range("B1").Select
Do Until Selection.Value = "xxx"
If Selection.Value = "" Then
Selection.EntireRow.Delete
Else
Selection.Offset(1, 0).Select
End If
Loop
Range("A1").Select
End Sub
If you have completed the free exercises "Free Basics", just copy/paste the macro above in the Visual
Basic editor and run it.
Exiting a Loop
In the loop above if you want the loop to stop when it finds the value 99 you can add this line of code
within the loop:
If Selection.Value = 99 Then Exit Do
Exit allows you to get out of almost anything like:
Exit Sub
http://www.excel-vba.com/excel-vba-solutions-intermediate.htm
9/13
11/12/2014
Exit For
Exit Do
Here is a sample of what you will find in lchapter20
of the downloadable Tutorial on Excel macros
10/13
11/12/2014
11/13
11/12/2014
Application.CutCopyMode = False
End Sub
Step 2: Enter values in cells A1 and B1 (your first and lat name for example).
Step 3: Run the macro
You end up with a Word document named testWord .Doc in the same directory as the Excel workbook
in which the macro runs. The Word document consists of a single sheet with a two cells table with the
values of cell A1 and B1 of the workbook.
Notice that you use VBA for Word within the object varDoc that you have created. If you do not know
VBA for Word remember that there is also a Macro Recorder in Word. The object varDoc can be visible
or you can work within it without bringing it on screen with:
varDoc.Visible = False
API Working with Windows
API stands for Application Programming Interface and consists of a collection of functions that provide
programmatic access to the features of the operating system (Windows). When you use API's within
VBA for Excel not only do you control Excel but also most parts of Windows.
To organize your discovery of Excel macros, the downloadable Tutorial on Excel Macros is
divided in three sections (all 3 sections part of the single download):
Section 1: Excel Macros Programming (Chapters 1 to 10)
This section is about recording, writing, modifying and testing macros in the Visual Basic Editor. You will also
learn about security and discover "events" (an event is what starts the macro).
Section 2: Excel VBA Vocabulary (Chapters 11 to 23)
Developing a macro is communicating with Excel and to do so you need to use a language called Visual Basic for
Applications (VBA). In section 2 you will learn all the VBA vocabulary that is essential to work with business data
(accounting, sales, production and others).
Section 3: Forms and Controls in VBA for Exce (Chapters 24 to 33)
The userform is a small or large dialog window that you create and allows the user to submit values that will be used
by your macros. To these userforms you will add controls (command buttons, text boxes, list boxes and others) and
program them.
http://www.excel-vba.com/excel-vba-solutions-intermediate.htm
12/13
11/12/2014
http://www.excel-vba.com/excel-vba-solutions-intermediate.htm
13/13