Professional Documents
Culture Documents
Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
Learning Macros
Why VBA Macros An Introduction
Where we write macros Introduction to VB Editor
Recording Macros
Personal Macro workbook
Declaring Variables
Interactive Macros - Msgbox / Inputbox
Decision Making (If, Else)
Looping (Do Loop, For Loop)
With VBA for Excel you can develop small procedures (macros) that
will make your professional life easier and allow you to do more in
less time.
With VBA for Excel you can develop a program that does EXACTLY
what you need and VBA is easy to learn.
Programming background is useful but not mandatory to learn
macros.
OR [ ALT + F11 ]
All VBA procedures (macros) start with Sub & end with
End Sub
You can run the code by pressing F5 or clicking the Run
symbol on the toolbar
Recording Macros
Excel provides Macro Recorder as the most user friendly tool.
It is very useful to see what properties and methods belong to various objects.
You can then use this as the basis for building your own code.
When you record a macro, a module is created and the code is written into it.
You can view this code and see exactly how Excel has tackled the problem in VBA.
You can modify this code or use it in other procedures.
You should not assume that a recorded macro will work under every
circumstance.
Look in the code window by pressing Alt-F11, and you will find that a new
module has been inserted that contains the code for the macro you
recorded
Decisions
Programs usually have to make decisions according to data
retrieved or input by the user.
Examples
When there is only one condition and one action, you will
use the simple statement:
If Application.ActiveCell = 5 Then
MsgBox "Cell is 5"
Else
MsgBox "Cell is not 5"
End If
Looping
Looping allows a block of code to be repeated until a condition or a
specified value is met.
Without looping facilities, programs would be extremely tedious and
difficult to maintain.
We use For next ,For each, Do Until, While wend etc. to reduce the code &
make it simple
Example
You wanted to display the numbers from 1 to 5. You could write the program as
follows:
MsgBox "1"
MsgBox "2"
Msgbox "3"
Msgbox "4"
MsgBox "5"
This code can be reduced and made easier to maintain by using the For..Next
looping statement as follows:
For n = 1 to 5
MsgBox n
Next n
For x = 1 To 100
If x = 50 Then
Exit For
End If
Next x
MsgBox x
End Sub
Examples
CutCopyMode
After each Copy/Paste operation, you should empty the clipboard with the following line of code to make
sure that the computer memory doesn't overload.
ActiveSheet.Paste
Application.CutCopyMode=False
DisplayAlerts
When you don't want Excel to ask you things like "A file already exists....." or "Do you want to save this file..."
you will use the following line of code at the beginning of your VBA procedure.
Application.DisplayAlerts = False
Then at the end
Application.DisplayAlerts = True
ScreenUpdating
When you don't want to see your screen follow the actions of your VBA procedure, you start and end your
code with the following sentences:
Application.ScreenUpdating = False
Application.ScreenUpdating = True
Examples
To Open a Workbook
Work ooks. ope
C:\Test.xls
Workbooks("book1").Worksheets("Sheet3").Activate
Workbooks("book1").Save
Activeworkbook.save
Workbooks("book1").Close
Activeworkbook.close
Exercise
Pre-work : Create a blank excel file in C drive with the name Income.xlsx
Tasks :
1. Open a workbook
2. Copy Incomestatement from current workbook
3. Paste Incomestatement in first sheet worksheet of Income.xlsx
4. Save workbook
5. Close workbook
Worksheets.add
Worksheets("Sheet1").tab.color = vbGreen
To rename a worksheet
Worksheets("Sheet1").Name = "Balance"
To select a worksheet
Worksheets("Balance").Activate
Worksheets("Sheet1").Visible= True
Worksheets("Sheet1").Visible= False
To delete a sheet
Worksheets("Sheet1").delete
Exercise
Tasks :
1. Add a new sheet
2. Name the sheet as income statement
3. Copy the income statement from previous sheet to new sheet
4. Change the tab color to green
5. Hide the previous sheet
" .ele t
" .ClearCo te ts
Ra ge F 6:H 6 . erge
Exercise
Tasks :
1. Copy and paste the income st. to different range in the same sheet
2. Color the header of the Income statement in red color
3. Color the particulars in green color
4. Merge the cells on the top
5. Add the title INCOME STATEMENT
6. Color the negative values in red color
Exercise :
Create a function to calculate tax by taking income and investment as
input and return tax payable as output
Inputs : Income , Investment
Output : Tax Payable
Sub proTestErrorHandler()
On Error GoTo addJump
Workbooks.Open "xxxxxx"
Exit Sub
addJump:
MsgBox "An error has occurred, call Peter at 1 514-257-0734"
End Sub
Example
Sub proTestErrorHandlerIgnore()
Write your code in lower case letters. If the spelling is right, VBE will
capitalize the necessary letters. If it doesn't.... check your spelling.
Declare all your variables (Dim) at the beginning of the procedure, it will
simplify the testing of your code.
You can insert a comment usi g i the co
the comment in green color
e t
Pivot Tables
It sums up large amount of information in a small amount of
space
Example
Memory Game
Macro
Pivot table
clearcontents
VBA
For Loop
Add
Inputbox
Do Loop
Value
Msgbox
If condition
Double
Dim
Macro Recorder
Font.color
Long
Function
Tab.color
Integer
Sub
Stop recording
String
Variables
Discussexcel.com
Workbooks
Worksheets
events
Range
Active cell
Visible
VB Editor
This workbook
Activate
Module
Merge
Alt+F11
Protection
Interior.color
Alt+F8
Resources
1. www.discussexcel.com
2. http://groups.google.com/group/excel-macros
3. www.ozgrid.com
4. www.cpearson.com
5. www.chandoo.org
6. www.exceluser.com
QUERIES ?