Professional Documents
Culture Documents
Introduction
Introducing Visual Basic for Applications
Understanding & Creating Modules
Introducing Visual Basic for Applications
Defining Procedures
End Sub
This is done through the use of the Public or Private keywords that I
introduce above and which are one of the optional elements of VBA
Sub procedures.
Since the default is that procedures are public, you actually don't need
to use the Public keyword. For example, the following VBA Sub
procedure, Del_Blanck_rows_1, is public. Note that there is no Public
keyword.
In both of the above cases, the VBA Sub procedures are public. In other
words, both are essentially the same.
Introducing Visual Basic for Applications
Sub Del_Blanck_rows()
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
In both of the above cases, the VBA Sub procedures are public. In other words, both are essentially the same.
private VBA Sub procedures, this means that they can only be accessed or called by those procedures that are
stored in the same VBA module.
Any procedures within any other module are not able to call it, even if those other modules are in the same
Excel workbook.
A lot of Excel users get confused when they are recording an Excel
Macro and they get prompted where they want to store their Excel
Macro in?
Introducing Visual Basic for Applications
Workbook & Worksheet Object
an object can contain another object, and that object can contain another
object, etc. In other words, Excel VBA programming involves working with
an object hierarchy
The mother of all objects is Excel itself. We call it the Application object.
The application object contains other objects. For example, the Workbook
object (Excel file).
This can be any workbook you have created. The Workbook object contains
other objects, such as the Worksheet object. The Worksheet object
contains other objects, such as the Range object.
Introducing Visual Basic for Applications
Example
Range("A1").Value = "Hello“
Application.Workbooks("create-a-macro").Worksheets(1).Range("A1").Value = "Hello"
Introducing Visual Basic for Applications
Collections
You may have noticed that Workbooks and Worksheets are both plural.
The Workbooks collection contains all the Workbook objects that are currently open.
Now let's take a look at some properties and methods of the Workbooks and Worksheets collection.
Properties are something which an collection has (they describe the collection), while methods do something (they
perform an action with the collection).
Note: the Add method of the Worksheets collection creates a new worksheet.
2. The Count property of the Worksheets collection counts the number of worksheets in a workbook.
MsgBox Worksheets.Count