Вы находитесь на странице: 1из 79

VISUAL BASIC APPLICATIONS AND

PROGRAMMING (150706)

MACROS: The First Step

Lecture Notes on VBA

Prof. Bhumika Patel


Asst. Professor,
I.T. Dept.,
SCET

VBA
BASIC : Beginners All-purpose
Instruction Code English words

Symbolic

Visual BASIC (VB) windows GUI, Drag & Drop


event driven programming by Microsoft
VB for Applications (VBA) Implementation of VB,
part of VB family

VBA
Programming language that can be built into
applications
Automate operations in applications like
MS Word, MS Excel, MS Powerpoint, Outlook, Access

Develops user-defined functions


Actions/Uses of VBA:
Record Macros
Write a code that performs actions a certain number
of times
Create user forms

VB Vs. VBA
VB

VBA

Programming Language

Implementation of VB

Applications are compiled

Applications are interpreted

Applications
ActiveX

compiled

into

Codes written
platforms

for

multiple

Builds stand-alone applications

Works with a host application

Programs quicker than VBA

Consumes much time to call


back functions

MACROS: Basics
Sequence of commands that we can repeat
Subprocedure/Subroutine

Run the macro to repeat the action by using a


single command
A Macro is a series of steps that is grouped
together as a single step and then executed when
necessary.

MACROS: Basics
MS Word, Excel, Powerpoint supports VBA Macro
Recorder
Created by
Writing VBA commands in VB Editor/
Turning on Macro Readers

Record/Edit/Delete Macros

Recording a Macro
Easiest way is to use Macro Recorder
Steps:

Switch on Macro Recorder


Assign method to run macro (optional)
Perform the actions you want in macro
Switch off Macro Recorder

Actions are recorded as instructions/codes in VBA


programming language

Recording a Macro
1.
2.
3.
4.
5.

Planning a Macro
Starting the Macro Recorder
Naming the Macro
Assigning the way to run the Macro
Stop recording the Macro

1. Planning a Macro

Recording a Macro

Setup the application


Make sure that the document/application is
opened with suitable text and contents
Activate the window containing the document
Ex: for saving operation, document must be
opened and has not been saved previously

2. Starting Macro Recorder

Recording a Macro

MS Office 2003:
Tools > Macro > Record New Macro

MS Office 2007:
Enable Developer Tab
MS Office Button > Word Options > Popular >
Check Show Developer Tab in the Ribbon
Select Developer Tab & click Record Macro

Check mark it

Record Macro

3. Naming the Macro

Recording a Macro

Name must start with a letter, can contain both


letters & numbers
Upto 80 characters long
Can contain underscores
Cannot contain spaces, punctuation, special
characters (like ! Or *)

3. Naming the Macro

Recording a Macro

Store Macro In:


All documents /
Current document

Description (optional) contains actions performed


by macro, when created, etc.

4. Assigning the way to run the Macro


Assign macro to

Recording a Macro

Button (Toolbar button)/


Keyboard

1. Button: Click Add , Modify & select Button


2. Keyboard: Enter shortcut key in Press new
shortcut key

Alt+(function key/regular key not used as menu key)


Ctrl+(function key/regular key)
Shift+(function key)
(Ctrl+Alt/Ctrl+Shift/Ctrl+Alt+Shift)+ (regular key/function
key)
Click Assign & Close

click

click

Toolbar button Icon

Press
Alt+Cnrl+Shift
+F

click

5. Stop Recording Macro


MS Office 2003:
Tools > Macro > Stop Recording

MS Office 2007:
Enable Developer Tab
Click Stop Recording

Recording a Macro

Running/Executing a Macro
MS Office 2003:
Tools > Macro > Macros
Select Macro > Run

MS Office 2007:
Select Developer Tab, Click Macro > Run /
Click icon on title bar /
Use the shortcut keyboard key

click

click

Deleting a Macro
MS Office 2003:
Tools > Macro > Macros
Select Macro > Delete

MS Office 2007:
Select Developer Tab, Click Macro > Delete

click

Limitations

Cannot be used to recover from errors


Difficult to use with looping
Cannot be used with DLL
Not included in professionally built-in applications
Not portable across all applications

Example Macro
A macro for replacing a word college by institute
in the document.

Click on Record Macro

Press Cntl+F

Click Replace All

Click Stop Recording

In new document, click on Macros

Select
the
Macro
for Find
and
Replace

Click

The word college will be replaced by


institute in whole document

Testing Macro in VB Editor


Select Macro option in Developer tab > Edit
Run macro by F5/Run Sub-UserForm /Run button

Stepping through a Macro


To see exactly what a Macro does
Arrange VB Editor & host application windows

Press [F8]/[Debug > Step Into] to step through the


macro command-by-command
Press [Debug > Step out] to step out of a macro

Recording the Word Macro


Create a Macro to transpose the current word with
the word to its right.
Type a statement in word document The quick
brown dog jumped over a lazy fox.
Record a macro
Select word quick by pressing F8 twice and then
press ESC key once.
Press Ctrl+X, go to end of word brown and press
Ctrl+V
Stop Macro
Click Edit and see the code

Recording the Word Macro


Create a Macro to transpose the current word with
the word to its right.
Sub Macro8()
' Macro8 Macro
Selection.Extend
Selection.Extend
Selection.EscapeKey
Selection.Cut
Selection.MoveRight Unit:=wdCharacter, Count:=6
Selection.PasteAndFormat (wdPasteDefault)
End Sub

Editing the Word Macro


Create a Macro to transpose the current word with
the word to its left.
Sub Macro9()
' Macro9 Macro
Selection.Extend
Selection.Extend
Selection.EscapeKey
Selection.Cut
Selection.MoveLeft Unit:=wdCharacter, Count:=6
Selection.PasteAndFormat (wdPasteDefault)
End Sub

Recording an Excel Macro: First Step


Create a Personal Macro Workbook if you dont have
one yet:
Click Record Macro
In option store Macro In, choose Personal Macro
Workbook
Start Recording the Macro
Type a single character in active cell
Stop recording
Choose [View > unhide] to unhide PERSONAL.XLSB
Delete the Macro you recorded
You have a Personal Macro Workbook that you can use

click

click

click

Recording an Excel Macro


Open MS Excel
Click Record Macro and name it as Workbook_with_Months
In option store Macro In, choose Personal Macro
Workbook
Start Recording the Macro
Open a blank workbook
Click cell A1 to select it, type January 2012 and press ->
key to go to cell B1
Type February 2012 in cell B1 and press <- to go to cell A1
Drag from cell A1 to L1 to display all months
Stop recording
Click
save
button
and
save
workbook
as
Sample_Workbook.xls
Delete Sample_Workbook.xls and then run the macro

click

click

Editing the Excel Macro


Choose View > Unhide to unhide Personal Macro
Workbook PERSONAL.XLSB

click

Opening Excel Macro for Editing


Click Macros in Developer Tab
Select the macro Workbook_with_Months and click
Edit button for editing in VB Editor

click

Editing the Excel Macro


Sub Macro4()
' Macro4 Macro
Range("A1").Select
ActiveCell.FormulaR1C1 = "Jan-2012"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Feb-2012"
Range("A1:B1").Select
Selection.AutoFill Destination:=Range("A1:L1"), Type:=xlFillDefault
Range("A1:L1").Select
End Sub

Editing the Excel Macro


Sub Macro4()
' Macro4 Macro
Range("A1").Select
ActiveCell.FormulaR1C1 = "Jan-2012"
Range("B1").Select
ActiveCell.FormulaR1C1 = "Feb-2012"
Range("A1:B1").Select
Selection.AutoFill Destination:=Range("A1:L1"), Type:=xlFillDefault
Range("A1:L1").Select
Continue

Editing the Excel Macro


Add the following code in Macro
Range("A2").Select
ActiveCell.FormulaR1C1 = 100
Range("B2").Select
ActiveCell.FormulaR1C1 = 200"
Range("A2:B2").Select
Selection.AutoFill Destination:=Range("A2:L2"), Type:=xlFillDefault
Range(" A2:L2 ").Select
End Sub

Editing the Excel Macro


Editing the Macro enters the months and
additionally enters values 100 through 1200 in
second row of cells of the worksheet.

Recording the Powerpoint Macro


Microsoft has removed the macro recorder tool from MS

Office Powerpoint 2007, so we need to create a macro


using VBA Code.
First Create a Macro Enabled powerpoint presentation.
Click the Macro Toolbar Button in Developer Tab
Enter the name for the macro (in our case Insert_Logo)
Store it in the Presentation1.pptm
Click the create button

Recording the Powerpoint Macro


This opens VB Editor
Enter the Code given below
ActiveWindow.Selection.SlideRange.Shapes.AddPicture(FileName:="G:\olympics_log
o.gif", LinkToFile:=msoFalse, SaveWithDocument:=msoCTrue, Left:=60, Top:=35,

Width:=98, Height:=48).Select

Save the Macro (Ctrl + S)


And then Run the Macro

Save as Macro Enabled


Presentation

Click to create a Macro

Click to create

Click to edit a Macro

Click to Run the Macro

Logo is placed in active slide

END

Lecture Notes on VBA

Prof. Bhumika Patel


Ass. Professor,
I.T. Dept.,
SCET

Вам также может понравиться