You are on page 1of 10

What is VBE/VBA in MS Excel?

The Visual Basic Editor (VBE) or Visual Basic for Applications (VBA) is a program
within Excel that allows you to communicate with Excel. It is a programming
language of Excel.
It is an integration of the Microsofts event-driven programming language Visual
Basic with Microsoft Office applications such as Microsoft Excel. By running VBA
within the Microsoft Office applications, you can build customized solutions and
programs to enhance the capabilities of those applications. With Excel VBA you
can automate tasks in Excel by writing macros.
How to create/record a macro?
To create a simple macro,

first open a blank excel spreadsheet


Go to file -> options -> customize ribbon.
Tick the Developer check box if its untick as shown below (right bottom
side of the picture).

Click on OK.
You can find the Developer tab next to the view tab.

On the Developer tab, you can find an option to Record Macro.


Click on Record Macro, you will see a window like this ->

Macro name: Macro name any name having a combination of alphabets


and numbers. Special characters (including spacing, comma and
semicolon) are not allowed in macro name. Also the macro name like
ABC1, XYZ1, etc. are not allowed below these are cell references in Excel.
The macro name cant start with number. The name like Macro1,
Copy_Paste, deletedata, UpdateData, etc. are the expectable macro
names.
Shortcut key: option gives you an option to create a short key for your
macro to run. Be very careful while you choose a short cut for your macro.
The shortcut key for your macro should not be any from those that already
exist in MS Excel/MS Office. For example Ctrl+V (paste), Ctrl+X (cut),
Ctlr+Z (undo), Ctrl+Y (redo), Ctrl+C (copy), etc. Hence suggestible not to
give any shortcut key for your macro. Leave it blank.
Store macro in: The place you would want to store your macro. There are
three location where you can save our macro -> They are Personal Macro
Workbook, New Workbook and This Workbook.

Always suggestible to save your macro is This workbook because other


two are to be only used if you are making a macro for someone else or if
you are a master in VBA who has an expert knowledge in VBA.
Description in: Here you can write the short/quick description about your
macro. This is optional. You can leave it blank.
Once you are done, click on OK.
The recording of macro has started. You can do your activity while the
macro recording is on.
Now click on Stop Recording.

Now click on Macros to view your macro and its code. Alternatively you
can also press Alt+F11. Alt+F11 is a shortcut key to VB editor window. In
this window click on Modules -> Module1 (default location). You will see
your macro and its code recorded here.

Or alternatively click on Edit and it will take you through the above
window.

This is how you record a macro. Any or every macro stars with Sub and
ends with Sub. For example
Sub Macro1()
'
' your activity/code
'
End Sub
To delete the macro, repeat the same step above, but click on Delete
button after selecting the macro you want to delete.

How to create a command button and assign a macro to it?


To place a command button on your worksheet and assign a macro to it, execute
the following steps

On the Developer tab, click Insert.


In the ActiveX Controls group, click Form Controls -> Button.

Drag the button on your worksheet.

Give a name to your button.

You can give any name. No rules for the button name.
To assgin a macro to this button, right on the button and click on assign
macro.
The list that will appear, choose the macro you want to assign to this
macro button.

Concept of variables
Variables are like mail boxes in the post office. The contents of the variables
changes every now and then, just like the mail boxes. In VBA, variables are areas
allocated by the computer memory to hold data. Like the mail boxes, each
variable must be given a name. To name a variable in VBA, you have to follow a
set of rules, as follows:
a) Variable Names: The following are the rules when naming the variables in VBA

It must be less than 255 characters


No spacing is allowed
It must not begin with a number
Period is not permitted

Examples of valid and invalid variable names are displayed in Table belowValid Name

Invalid Name

My_Car

My.Car

ThisYear

1NewBoy

Long_Name_Can_beUSE

He&HisFather
acceptable

*& is not

Group88

Student ID
allowed

* Spacing not

b) Declaring Variables: In VBA, one needs to declare the variables before using
them by assigning names and data types. There are many VBA data types, which
can be grossly divided into two types, namely the numeric data types and nonnumeric data types.
i) Numeric Data Types
Numeric data types are types of data that consist of numbers, which can be
computed mathematically with various standard operators such as add, minus,
multiply, divide and so on. In VBA, the numeric data are divided into 7 types,
which are summarized in Table below

Type

Storag
Range of Values
e

Byte

1 byte

Integer

2 bytes -32,768 to 32,767

Long

4 bytes -2,147,483,648 to 2,147,483,648

Single

4 bytes

Double

-1.79769313486232e+308 to -4.94065645841247E-324 for


negative values
8 bytes
4.94065645841247E-324 to 1.79769313486232e+308 for positive
values.

0 to 255

-3.402823E+38 to -1.401298E-45 for negative values


1.401298E-45 to 3.402823E+38 for positive values.

Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807

Decimal

12
bytes

+/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use


+/- 7.9228162514264337593543950335 (28 decimal places).

ii) Non-numeric Data Types


The nonnumeric data types are summarized in Table below
Data Type

Storage

Range

String(fixed length)

Length of string

1 to 65,400 characters

String(variable length) Length + 10 bytes

0 to 2 billion characters

Date

8 bytes

January 1, 100 to December 31, 9999

Boolean

2 bytes

True or False

Object

4 bytes

Any embedded object

Variant(numeric)

16 bytes

Any value as large as Double

Variant(text)

Length+22 bytes

Same as variable-length string

Examples of declaring a variable/s:


Dim i as integer
Dim x as string
Dim dat as Date
So on.
Message Box
A message box normally act as a dialog box where users can interact with the
computer, it is able to perform certain actions in response to what the user clicks
or selects.
The format for a message box is as follows:
Message = MsgBox(Prompt, Style Value,Title)
Example: yourMsg=MsgBox( "Click OK to Proceed", 1, "Startup Menu") &
yourMsg=Msg("Click OK to Proceed". vbOkCancel,"Startup Menu")

Sub CommandButton1_Click()
Dim YourMsg As String
Cells(1, 2) = "Your first VBA program"
YourMsg = Cells(1, 2)
MsgBox YourMsg
End Sub

Sub CommandButton1_Click()
Dim YourMsg As String
Cells(1, 2) = "Your first VBA program"
YourMsg = Cells(1, 2)
MsgBox YourMsg, vbYesNoCancel

The arguments works as follows1) Prompt, will display the message in the message box.
2) The Style Value determines what type of command button will appear in
the message box.
3) The Title argument will display the title of the message board.
4) Message is a variable that holds values that are returned by the MsgBox ( )
function.
The values are determined by the type of buttons being clicked by the users. It
has to be declared as Integer data type in the procedure or in the general
declaration section.
In this example, I create three command buttons which show different Options. I
put in a bit of program codes in the last button which involve the use of
If...Then...Elseif statements. The various statements will be discussed in the later
part of the document.

Style Values and Command Buttons


Style Value Named Constant

Button Displayed

vbOkOnly

Ok button

vbOkCancel

Ok and Cancel buttons

vbAbortRetryIgnore

Abort, Retry and Ignore buttons.

vbYesNoCancel

Yes, No and Cancel buttons

vbYesNo

vbRetryCancel

Yes and No buttons


Retry and Cancel buttons

To make the message box looks more sophisticated, you can add an icon beside
the
message. There are four types of icons available in VBE
Value Named Constant

Example:

16

vbCritical

32

vbQuestion

48

vbExclamation

64

vbInformation

Sub CommandButton1_Click()
Dim YourMsg As String
Cells(1, 2) = "Your first VBA program"
YourMsg = Cells(1, 2)
MsgBox YourMsg, vbYesNoCancel + vbExclamation
End Sub
Exercise for practise
1) Write down the steps to add a Developer tab to your Menu bar in a excel
workbook.
2) Write down the steps to record a simple basic excel vba macro.
3) What should be the code to display a message Hello with an OK
button?
4) Provide a code to display a message Welcome to Nepa with button
namely Yes, No and Cancel.
5) A code for user to input a value or a message. After user enters the value
or a message, provide an option for OK and Cancel button. Once users
clicks on OK, the value or a message would then be displayed in cell B2.
6) Record a Macro to copy paste the data from a range of cells to another
range of cells. For example I want to copy and paste the data from range
A1:E10 to range K1:O10.
7) Considering the same data above in question 6, but copy it in the sheet2.
Write a macro for this task.
8) Macro to colour code the every alternate cell value. You can choose any
colour of your choice.
9) Macro to auto fill the values from 1 to 10 in cell A1 to A10.
10)
Syntax or a code line to perform the actions like
a) Ctrl+Shift+down arrow key
b) Ctrl+Shift+up arrow key
c) Ctrl+Shift+left arrow key
d) Ctrl+Shift+right arrow key
e) Copy/paste/delete the values from cell/cells, range/ranges
f) Ctrl+home
g) Ctrl+end
h) Ctrl+down arrow key
i) Ctrl+up arrow key
j) Ctrl+left arrow key
k) Ctrl+right arrow key
l) To reference a particular worksheet/workbook
m) To reference a particular sheet within workbook and between the
workbooks.

You might also like