Professional Documents
Culture Documents
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,
Click on OK.
You can find the Developer tab next to the view tab.
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.
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
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
Long
Single
4 bytes
Double
0 to 255
Decimal
12
bytes
Storage
Range
String(fixed length)
Length of string
1 to 65,400 characters
0 to 2 billion characters
Date
8 bytes
Boolean
2 bytes
True or False
Object
4 bytes
Variant(numeric)
16 bytes
Variant(text)
Length+22 bytes
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.
Button Displayed
vbOkOnly
Ok button
vbOkCancel
vbAbortRetryIgnore
vbYesNoCancel
vbYesNo
vbRetryCancel
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.