Top Banner

of 79

MACROS_BP

Jun 02, 2018

Download

Documents

Palak Ariwala
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/10/2019 MACROS_BP

    1/79

    MACROS: The First Step

    Prof. Bhumika Patel

    Asst. Professor,

    I.T. Dept.,

    SCETLecture Notes on VBA

    VISUAL BASIC APPLICATIONS AND

    PROGRAMMING (150706)

  • 8/10/2019 MACROS_BP

    2/79

    VBA

    BASIC : Beginners All-purpose SymbolicInstruction Code English words

    Visual BASIC (VB) windows GUI, Drag & Dropevent driven programming by Microsoft

    VB for Applications (VBA)Implementation of VB,part of VB family

  • 8/10/2019 MACROS_BP

    3/79

    VBA

    Programming language that can be built intoapplications

    Automate operations in applications like

    MS Word, MS Excel, MS Powerpoint, Outlook, AccessDevelops user-defined functions

    Actions/Uses of VBA:

    Record Macros

    Write a code that performs actions a certain numberof times

    Create user forms

  • 8/10/2019 MACROS_BP

    4/79

    VB Vs. VBA

    VB VBA

    Programming Language

    Applications are compiled

    Applications compiled intoActiveX

    Builds stand-alone applications

    Programs quicker than VBA

    Implementation of VB

    Applications are interpreted

    Codes written for multipleplatforms

    Works with a host application

    Consumes much time to callback functions

  • 8/10/2019 MACROS_BP

    5/79

    MACROS: Basics

    Sequence of commands that we can repeat

    Subprocedure/Subroutine

    Run the macro to repeat the action by using asingle command

    A Macro is a series of steps that is groupedtogether as a single step and then executed when

    necessary.

  • 8/10/2019 MACROS_BP

    6/79

    MACROS: Basics

    MS Word, Excel, Powerpoint supports VBA MacroRecorder

    Created by Writing VBA commands in VB Editor/

    Turning on Macro Readers

    Record/Edit/Delete Macros

  • 8/10/2019 MACROS_BP

    7/79

    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 VBAprogramming language

  • 8/10/2019 MACROS_BP

    8/79

    Recording a Macro

    1. Planning a Macro

    2. Starting the Macro Recorder

    3. Naming the Macro

    4. Assigning the way to run the Macro5. Stop recording the Macro

  • 8/10/2019 MACROS_BP

    9/79

    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 beopened and has not been saved previously

  • 8/10/2019 MACROS_BP

    10/79

    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 >CheckShow Developer Tab in the Ribbon

    Select Developer Tab & click Record Macro

  • 8/10/2019 MACROS_BP

    11/79

  • 8/10/2019 MACROS_BP

    12/79

    Check mark it

  • 8/10/2019 MACROS_BP

    13/79

    Record Macro

  • 8/10/2019 MACROS_BP

    14/79

    3. Naming the Macro Recording a Macro

    Name must start with a letter, can contain bothletters & numbers

    Upto 80 characters long

    Can contain underscoresCannot contain spaces, punctuation, special

    characters (like ! Or *)

  • 8/10/2019 MACROS_BP

    15/79

    3. Naming the Macro Recording a Macro

    Store Macro In:

    All documents /

    Current document

    Description (optional) contains actions performedby macro, when created, etc.

  • 8/10/2019 MACROS_BP

    16/79

  • 8/10/2019 MACROS_BP

    17/79

    4. Assigning the way to run the Macro

    Recording a MacroAssign macro to

    Button (Toolbar button)/

    Keyboard

    1. Button: Click Add , Modify & select Button2. Keyboard: Enter shortcut key in Press new

    shortcutkey 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/functionkey)

    Click Assign & Close

  • 8/10/2019 MACROS_BP

    18/79

    click

  • 8/10/2019 MACROS_BP

    19/79

    click

  • 8/10/2019 MACROS_BP

    20/79

    Toolbar button Icon

  • 8/10/2019 MACROS_BP

    21/79

    PressAlt+Cnrl+Shift+F

    click

  • 8/10/2019 MACROS_BP

    22/79

    5. Stop Recording Macro Recording a Macro

    MS Office 2003:

    Tools > Macro > Stop Recording

    MS Office 2007: Enable Developer Tab

    Click Stop Recording

  • 8/10/2019 MACROS_BP

    23/79

    Running/Executing a Macro

    MS Office 2003:

    Tools > Macro > Macros

    Select Macro > Run

    MS Office 2007:

    SelectDeveloper Tab, Click Macro > Run /

    Click icon on title bar /

    Use the shortcut keyboard key

  • 8/10/2019 MACROS_BP

    24/79

    clickclick

  • 8/10/2019 MACROS_BP

    25/79

    Deleting a Macro

    MS Office 2003:

    Tools > Macro > Macros

    Select Macro > Delete

    MS Office 2007:

    SelectDeveloper Tab, Click Macro > Delete

  • 8/10/2019 MACROS_BP

    26/79

    click

  • 8/10/2019 MACROS_BP

    27/79

    Limitations

    Cannot be used to recover from errors

    Difficult to use with looping

    Cannot be used with DLL

    Not included in professionally built-in applicationsNot portable across all applications

  • 8/10/2019 MACROS_BP

    28/79

    Example Macro

    A macro for replacing a wordcollegebyinstitutein the document.

  • 8/10/2019 MACROS_BP

    29/79

    Click on Record Macro

  • 8/10/2019 MACROS_BP

    30/79

    Press Cntl+F

    Click Replace All

  • 8/10/2019 MACROS_BP

    31/79

  • 8/10/2019 MACROS_BP

    32/79

    Click Stop Recording

  • 8/10/2019 MACROS_BP

    33/79

    In new document, click on Macros

  • 8/10/2019 MACROS_BP

    34/79

    SelecttheMacrofor Findand

    Replace

    Click

  • 8/10/2019 MACROS_BP

    35/79

    The word college will be replaced byinstitute in whole document

  • 8/10/2019 MACROS_BP

    36/79

    Testing Macro in VB Editor

    Select Macro option in Developertab > Edit

    Run macro by F5/Run Sub-UserForm /Run button

  • 8/10/2019 MACROS_BP

    37/79

    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 themacro command-by-command

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

  • 8/10/2019 MACROS_BP

    38/79

    Recording the Word Macro

    Create a Macro to transpose the current word withthe word to its right.

    Type a statement in word document The quickbrown dog jumped over a lazy fox.

    Record a macroSelect word quickby pressing F8 twice and then

    press ESC key once.

    Press Ctrl+X, go to end of word brown and pressCtrl+V

    Stop MacroClick Edit and see the code

  • 8/10/2019 MACROS_BP

    39/79

    Recording the Word Macro

    Create a Macro to transpose the current word withthe word to its right.

    Sub Macro8()' Macro8 Macro

    Selection.Extend

    Selection.Extend

    Selection.EscapeKey

    Selection.CutSelection.MoveRight Unit:=wdCharacter, Count:=6

    Selection.PasteAndFormat (wdPasteDefault)

    End Sub

  • 8/10/2019 MACROS_BP

    40/79

    Editing the Word Macro

    Create a Macro to transpose the current word withthe word to its left.

    Sub Macro9()' Macro9 Macro

    Selection.Extend

    Selection.Extend

    Selection.EscapeKey

    Selection.CutSelection.MoveLeft Unit:=wdCharacter, Count:=6

    Selection.PasteAndFormat (wdPasteDefault)

    End Sub

  • 8/10/2019 MACROS_BP

    41/79

  • 8/10/2019 MACROS_BP

    42/79

  • 8/10/2019 MACROS_BP

    43/79

  • 8/10/2019 MACROS_BP

    44/79

  • 8/10/2019 MACROS_BP

    45/79

    Recording an Excel Macro: First Step

    Create a Personal Macro Workbook if you donthaveone yet:

    Click Record Macro

    In option store Macro In, choose Personal MacroWorkbook

    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

  • 8/10/2019 MACROS_BP

    46/79

  • 8/10/2019 MACROS_BP

    47/79

    click

  • 8/10/2019 MACROS_BP

    48/79

  • 8/10/2019 MACROS_BP

    49/79

    click

  • 8/10/2019 MACROS_BP

    50/79

    click

  • 8/10/2019 MACROS_BP

    51/79

    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 January2012and press ->

    key to go to cell B1

    Type February 2012 in cell B1 and press

  • 8/10/2019 MACROS_BP

    52/79

    click

  • 8/10/2019 MACROS_BP

    53/79

    click

  • 8/10/2019 MACROS_BP

    54/79

  • 8/10/2019 MACROS_BP

    55/79

    Editing the Excel Macro

    Choose View > Unhide to unhide Personal MacroWorkbookPERSONAL.XLSB

  • 8/10/2019 MACROS_BP

    56/79

    click

  • 8/10/2019 MACROS_BP

    57/79

    Opening Excel Macro for Editing

    Click Macros in Developer Tab

    Select the macroWorkbook_with_Monthsand click

    Edit button for editing in VB Editor

  • 8/10/2019 MACROS_BP

    58/79

    click

  • 8/10/2019 MACROS_BP

    59/79

  • 8/10/2019 MACROS_BP

    60/79

    Editing the Excel Macro

    Sub Macro4()' Macro4 Macro

    Range("A1").Select

    ActiveCell.FormulaR1C1 = "Jan-2012"

    Range("B1").SelectActiveCell.FormulaR1C1 = "Feb-2012"

    Range("A1:B1").Select

    Selection.AutoFill Destination:=Range("A1:L1"), Type:=xlFillDefault

    Range("A1:L1").Select

    End Sub

  • 8/10/2019 MACROS_BP

    61/79

    Editing the Excel Macro

    Sub Macro4()' Macro4 Macro

    Range("A1").Select

    ActiveCell.FormulaR1C1 = "Jan-2012"

    Range("B1").SelectActiveCell.FormulaR1C1 = "Feb-2012"

    Range("A1:B1").Select

    Selection.AutoFill Destination:=Range("A1:L1"), Type:=xlFillDefault

    Range("A1:L1").Select

    Continue

  • 8/10/2019 MACROS_BP

    62/79

    Editing the Excel Macro

    Add the following code in MacroRange("A2").Select

    ActiveCell.FormulaR1C1 =100

    Range("B2").Select

    ActiveCell.FormulaR1C1 =200"

    Range("A2:B2").SelectSelection.AutoFill Destination:=Range("A2:L2"), Type:=xlFillDefault

    Range(" A2:L2").Select

    End Sub

  • 8/10/2019 MACROS_BP

    63/79

  • 8/10/2019 MACROS_BP

    64/79

  • 8/10/2019 MACROS_BP

    65/79

    Editing the Excel Macro

    Editing the Macro enters the months andadditionally enters values 100 through 1200 in

    second row of cells of the worksheet.

  • 8/10/2019 MACROS_BP

    66/79

    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

  • 8/10/2019 MACROS_BP

    67/79

    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

  • 8/10/2019 MACROS_BP

    68/79

  • 8/10/2019 MACROS_BP

    69/79

    Save as Macro EnabledPresentation

  • 8/10/2019 MACROS_BP

    70/79

    Click to create a Macro

  • 8/10/2019 MACROS_BP

    71/79

    Click to create

  • 8/10/2019 MACROS_BP

    72/79

    Click to edit a Macro

  • 8/10/2019 MACROS_BP

    73/79

  • 8/10/2019 MACROS_BP

    74/79

  • 8/10/2019 MACROS_BP

    75/79

    Click to Run the Macro

  • 8/10/2019 MACROS_BP

    76/79

  • 8/10/2019 MACROS_BP

    77/79

  • 8/10/2019 MACROS_BP

    78/79

    Logo is placed in active slide

  • 8/10/2019 MACROS_BP

    79/79

    END

    Prof. Bhumika Patel

    Ass. Professor,