You are on page 1of 13

Excel VBA

Introduction
Introducing Visual Basic for Applications
Understanding & Creating Modules
Introducing Visual Basic for Applications
Defining Procedures

A procedure is the block of statements that is enclosed by a particular


declaration statement and End declaration.
Private Sub Test()

End Sub

The main purpose of a procedure is to carry out a particular task or


action.
Introducing Visual Basic for Applications
Calling Procedures

When creating a VBA Sub procedure you have the option of


determining which other procedures are able to call it.

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.

However, let's take a deeper look at what is the meaning of public or


private procedures, and how can you determine whether a particular
VBA Sub procedure is public or private
Introducing Visual Basic for Applications
Public VBA Sub Procedures
VBA Sub procedures are, by default, public. When a particular
procedure is public, there are generally no access restrictions.

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

Public Sub Del_Blanck_rows_1()


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.

Private Sub Del_Blanck_rows2()


Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
End Sub
Introducing Visual Basic for Applications
Where to store macros?

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“

Means below lines of code with object hierarchy

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.

That is because they are collections.

The Workbooks collection contains all the Workbook objects that are currently open.

The Worksheets collection contains all the Worksheet objects in a workbook.


Introducing Visual Basic for Applications
You can refer to a member of the collection, for example, a single Worksheet object, in
three ways.

1. Using the worksheet name.


Worksheets("Sales").Range("A1").Value = "Hello"
2. Using the index number (1 is the first worksheet starting from the left).
Worksheets(1).Range("A1").Value = "Hello"
3. Using the CodeName.
Sheet1.Range("A1").Value = "Hello"
Introducing Visual Basic for Applications
Properties and Methods

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

1. The Add method of the Workbooks collection creates a new workbook.


Workbooks.Add

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

Result when you click the command button on the sheet:


Note: the Count property of the Workbooks collection counts the number of active workbooks.
Introducing Visual Basic for Applications
https://docs.microsoft.com/en-us/office/vba/api/overview/excel

You might also like