P. 1
Vba for Excel Made Simple

Vba for Excel Made Simple


|Views: 3,692|Likes:
Published by fmunir1

More info:

Published by: fmunir1 on Aug 18, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less





In the real world, we use objects like televisions, mobile phones, compact
disks and books everyday. In the same way, Excel and VBA uses objects –
albeit non-physical in nature. If we think, for example, about a television set:
the television has characteristics or properties associated with it, such as the
size, the make, model, weight and colour. You can also do things to control
the behaviour of this object, such as turn it on or off, adjust the volume, adjust
the colour contrast or kick it (if it is annoying you that much). In VBA, we
can refer to Excel/VBA objects in much the same way: that is, objects have
properties associated with them and have things that can be done to them.
The latter are called methods. In the case of Excel/VBA, the objects could
be things like workbooks, worksheets, charts and cell ranges. We will look
at properties and methods for these Excel/ VBA objects in the next sections.
Those described in the next sections are not complete by any means.

Some properties of objects

As stated earlier, a property is a characteristic of an object – or a way of
describing some aspect of it. When we try to describe the TV set we usually
refer to its dimensions, colour, make, model, and so on. Moreover, these
properties will have values associated with them, such as the size is a 28”
screen, or the make might be a Sony television. In a similar way, when we
describe properties of Excel objects, such as a range of cells, then properties
of this object would be things like the name of the cell range, the cell width,
and so on. A Range object in Excel is defined as a row, column, combination
of rows and/or columns, a selection of many – not necessarily contiguous –
cells, or even one cell.
Another example of an Excel object is a Workbook, and this would have
properties such as the workbook name (there must be a name since it is a
synonym for an Excel file), or password (i.e. a property that gives password
access to the workbook). You can see from these examples that the
properties of objects will differ, although there may be some in common.
Each occurrence of each object has its own properties which you can look
at and change. We will see how to do this later in this chapter. Examples of
other properties typical VBA /Excel objects are given in Table 4.1.



Some properties and typical values

Some methods


Name identifies the range,for example

Select selects a worksheet range to


work with in some way

Column lets the user know the number

ClearContents clears data from a

of the first column in the Range object

range of selected cells.

Formula shows the user the formula

Copy, Cut and Paste are more examples

in a Range object

of range methods

WorksheetName identifies the worksheet. For

Select selects the worksheet object

example, Weeklysales

Delete deletes a worksheet from the

Visible indicates whether the worksheet

currently open workbook

is visible (i.e. active) – possible values

Protect protects the active sheet from

are true or false (Boolean values)

any changes.

WorkbookName identifies the workbook, for

Save used with this object, can be


in the form Save or SaveAs: Save

ReadOnly – if true, the workbook is read

requires no arguments,SaveAs takes

only; if false, changes can be made to it

arguments such as FileName to use,
File format – format of the file, e.g. .xls, xlaOpen takes the argument FileName
Password – true if password protected.


Name identifies the chart,

Location – a chart can be embedded

for example myChart.

in a worksheet or placed on a separate

ChartType – the chart type, e.g. pie, bar

chart sheet

HasLegend makes the legend of a chart

PlotArea refers to the colour of the plot

visible if set to true.

area of a chart.

Excel methods

A method is an action that can be performed with an object. For example,
controlling the volume on a television set is a method that can be performed
with a real-world object. The behaviour of any object can be controlled by
using its methods. If we consider the Range object, you can do things to this
object such as Select it to work with in some way, or you can ClearContents
(deleting the cells in the range). You can also Copy the contents to the
Clipboard, and so on. If we consider another object such as a worksheet,
again, you could Select it to work with it, and you could also Delete it –

Table 4.1 Summary of object properties and methods


removing it from the currently open workbook, Protect it – i.e. prevent
changes being made to it. Thus, methods, like properties, will differ from
object to object, though objects of the same type will clearly share methods.

Self-assessment exercise

Give three examples of properties and methods of a motor car object.

Referencing an object’s properties and methods

We reference the properties and methods of an object by using the dot
notation (.), it is written in the following notation:


Where Object is a reference to an object, such as Range or Workbook, and
Identifier could be a valid property or method, such as Name or ClearContents.

Range ("my_cells").ClearContents

is a reference to the ClearContents method of the Range object my_cells.

Worksheets("that_sheet").Range ("C1").value = 6.

In this example, property value 6 has been set in the cell range C1, in the
worksheet called that_sheet of the Worksheets collection (see next section).
Note that in this example, the worksheet reference has been included. When
you need to refer to a specific worksheet, you have to include the worksheet
reference. Otherwise, the active worksheet reference would be taken.

Charts ("Chart1").PlotArea.Interior.ColorIndex = 3.

In this example, the colour of the PlotArea of chart1 of the charts collection
(see next section) has been set to red (ColorIndex = 3).


An event is something that can happen to an object. For example, clicking
a button in a dialog box is an event. When a button is clicked, some action
will follow, such as executing a macro. VBA for Excel is an event-driven
language. We will be returning to event-driven programming later on.


You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->