Sunteți pe pagina 1din 23

GE, Canada

Best Practice Sharing - Macros -


Gearing up to succeed in a data driven environment
2 /
Best Practice Sharing
Forward
This Excel document was created on the basis of my experiences lived through various situations at GE.
I understand there might be numerous ways to perform tasks in Excel, and this document does not pretend
to be completely comprehensive, but merely thoughts and guides based on personal experience.
Special thanks to Melissa Kay for her precious help in making this document as perfect and straight forward
as possible.
Please do not hesitate to contact me for any comments, suggestions, or if you need help with Excel. You can
send me an email at guillaume.cote-provencher@ge.comand I will gladly reply.
Thank you for your interest in this document. Best of luck in your future projects.
Guillaume Cote-Provencher | FMP
3 /
Best Practice Sharing
Table of Contents
Module 5: Macro Automating Workflow
5.1. Introduction
5.1.1. Definition
5.1.1.1. What Is A Macro
5.1.1.2. Terminology
5.1.2. Important Points
5.2. Recording A Macro
5.2.1. Basics
5.2.2. Steps
5.2.2.1. Preparation
5.2.2.2. Recording
5.2.2.3. Inserting Button
5.2.2.4. Editing
5.3. VBA View
5.3.1. Basics
5.3.2. Deciphering the VBA Language
5.3.2.1. Explaining sequence of codes
5.3.3. Examples
4 /
Best Practice Sharing
Table of Contents
Module 5: Macro Automating Workflow
5.4. Trouble Shooting
5.4.1. Basics
5.4.2. Not All the Files Are Open
5.4.3. Change In File Names
5.4.4. Change In File Structure
5.4.5. Sequencing and Calculation
5 /
Best Practice Sharing
5.1. Introduction
6 /
Best Practice Sharing
Module 5: Macros
5.1. Introduction
5.1.1. Definition
A Macro is a set of codes referring to precise actions performed in Excel
Once a Macro is recorded /written you can run it as often as you like
Running a Macro will have Excel perform every task coded in the Macro
Macros are useful to automate repetitive tasks
Macros can make Excel take care of hundreds of operations in a single click
Macros are written in VBA language (programming)
5.1.2. Important Points
5.1.1.1. What Is A Macro
5.1.1.2. Terminology
Recording a Macro: Saving performed operations into a Macro, making it possible to repeat the
same tasks at a later time
Running a Macro: Having the Macro repeat the previously saved operations
In Office 2010, not all Excel workbooks are macro enabled to ensure your Excel file is able to
operate Macros save your Excel workbook in: Excel Macro-Enabled Workbook (.*xlsm)
The fact that a Macro runs operations on its own makes it difficult to identify possible mistakes
by doing the operations manually you would be able to identify outlying values the Macro will
not tell you if the results dont make sense
7 /
Best Practice Sharing
Module 5: Macros
5.1. Introduction
A Macro is NOT flexible i.e. Macros will not adjust to structural changes in files/templates
Structural Changes: if a file has a different name, different tabs, different # of
rows/columns, etc
If there is a change in a file name the Macro will not recognize the new name
If a value originally in cell A1 is now in B1 the Macro will still take the value it
finds in A1 to perform its operations
Make sure to adapt the Macro to any modifications made to files/templates
To circumvent potential issues related to file names you can save as recurrent files giving them
a generic name
Weekly files often have weekly variations in their names
Instead of modifying the Macro every week to fit the new names you might want
the Macro to consider a file with a generic name which you will update weekly
e.g. of a Macro
Codes describing
different operations
performed by Excel
A Macro is a set of
codes relating to a
set of tasks in Excel
Codes can be
recorded of manually
written
8 /
Best Practice Sharing
5.2. Recording A Macro
9 /
Best Practice Sharing
Module 5: Macros
5.2. Recording A Macro
5.2.1. Basics
Macros can be recorded or written
Recorded Macros: Excel will record your actions and convert them into a Macro
code
Written Macros: It is also possible to manually write the code for a Macro
Unless you know the VBA language very well, it is easier to record a Macro it is easier to modify
the codes in a recorded Macro than to write the codes from start to finish
Make sure all the Excel files you require to perform your operations are open before starting to
record a Macro
5.2.2. Steps
Open all Excel files required to perform the operations you want to code in a Macro
Make sure the sequencing of the operations to be performed has been tested before recording
Recording a Macro will save the operations in the exact same sequence as they are
performed while you record it
Be mindful of operations that need to be performed before others when you record a
Macro
Some calculations need to be executed before others and this order should be
considered when recording make sure formulas are refreshed accordignly
5.2.2.1. Preparation
10 /
Best Practice Sharing
Module 5: Macros
5.2. Recording A Macro
Record Macro box
Enter the name you want to give your Macro
Make sure the name is meaningful it will
make it easier to select the Macro from a list
You can store the Macro in the file from which you
selected the Macro menu from, or in a new
workbook
You can store it in a new workbook just to
make it clearer to identify
Some choose the new workbook to only
contain the Run Macro button to make it
easier to use
Select OK
The Macro is now recording
When a Macro is recording you can see a record symbol in the bottom left hand corner of the Excel
files
Perform your Excel operations
Select the Stop symbol to end recording
Symbol indicating that the Macro is
running
Click on this symbol to stop the
recording
Select the View quick bar menu
Select Macros
Select Record Macro
Select a Name for the Macro
Select where you want to Store the Macro in:
5.2.2.2. Recording
11 /
Best Practice Sharing
Module 5: Macros
5.2. Recording A Macro
5.2.2.3. Inserting Button
A Button is a shape linked to a Macro that allows you to start running the Macro it is associated
with just by clicking on it
To insert a Button you need the Developer quick bar menu (does not come standard)
Insert Developer quick bar menu:
Select Options from the Excel file menu
Select Customize Ribbon
Check Developer from the right hand side menu
Developer quick bar menu is now activated
12 /
Best Practice Sharing
Module 5: Macros
5.2. Recording A Macro
Insert Button
Select Developer quick bar menu
Select Insert
Select Button
Draw the Button
Upon drawing of the Button a menu will open, allowing you to select the Macro
you want to tie the Button to
Right click on the Button to format font and rename
Click to insert Button
Double click on the Macro
you want to assign to the
Button
Notes and Observations
Every time you will click on the button it will initiate the Macro
Example of a Button linked to a
Macro designed to refresh all
Pivot Tables within a file
By clicking on the Button you
will start the Macro
13 /
Best Practice Sharing
Module 5: Macros
5.2. Recording A Macro
After having recorded a Macro you might want to edit it
You may want to simplify the writing
You may want to insert comments to help understand the codes
Select the View quick bar menu
Select Macros
Select View Macros
Select a the Macro you want to edit from the menu list
Select Edit to open the VBA view
5.2.2.4. Editing
Macros can be edited by manually writing new codes
It also possible to arrange the disposition of the codes in the VBA view
Notes and Observations
14 /
Best Practice Sharing
5.3. VBA View
15 /
Best Practice Sharing
Module 5: Macros
5.3. VBA View
5.3.1. Basics
The VBA view allows you to interact with existing macros
The VBA view allows you to see, edit, modify the Macro codes
Select Edit from the View Macros menu box to access VBA view
Notes and Observations
Once a Macro is recorded and saved, it is not possible to record new codes following the first recording
However, it is possible to modify an already recorded Macro by writing additional VBA codes without
having to record them
If you are not too sure of how to write an additional set of codes you can record a test Macro to see
how Excel records the additional operations performed you can then copy the codes from the test
Macro and paste them in the master Macro
If an operation in a Macro is very repetitive (i.e. if you always copy and paste from the same files you
can copy and paste the codes corresponding to the action of copying and pasting and then manually
modify the cell range
The 2 red
boxes
represent
the same
operation
type: Copy
and Paste
only the cell
areas are
different
You could Copy and Paste it in you could then modify the code to adjust the cell
areas S9:T9 for S12:12 and BL8:BM8 for BL9:BM9


16 /
Best Practice Sharing
Module 5: Macros
5.3. VBA View
5.3.2. Deciphering the VBA Language
Below is a view from a Macro set of codes
A Macro starts with Sub and the name of the Macro
Having a line start with makes the Macro to
ignore the line the Macro will not consider text
starting with as code this allows you to input
comments
A Macro ends with End Sub
Codes describing the
operations performed by the
Macro
By recording a Macro,
Excel will save the
operations into codes like
these

Notes and Observations
Sub and End Sub are automatically inputted when recording a Macro
The previous example shows a Macro that has been recorded and then edited to simplify the
understanding
17 /
Best Practice Sharing
Module 5: Macros
5.3. VBA View
5.3.2.1. Explaining sequence of codes
Windows("Pipeline Report.xlsm").Activate
Sheets("Current Pipeline").Select
Range("S12:T12").Copy
Windows("War Room.xlsx").Activate
Sheets("CANADA").Select
Range("BL9:BM9").Select
Selection.PasteSpecial Paste:=xlValues,
Operation:=xlNone, SkipBlanks:=False,
Transpose:=False
Go to file called Pipeline Report.xlsm
Go to tab called Current Pipeline
Select cells S12 to T12 AND Copy
Go to file called War Room.xlsx
Go to tab called CANADA
Select cells BL9 to BM9
Paste selection: Value, without any operation,
without skipping blanks, without transposing
VBA Literal
It is important to understand the VBA language to:
Modify Macros without having to re-record them completely
Lighten the codes by erasing unnecessary lines
Identify potential errors
Notes and Observations
18 /
Best Practice Sharing
Module 5: Macros
5.3. VBA View
5.3.3. Examples
Below is a view from a Macro set of codes UNEDITED
Codes for scrolling in the Excel file scrolling codes are NOT
necessary for the Macro they only crowd and slow down
the Macro execution scrolling codes can be deleted
Codes in recorded Macros can be difficult to understand due to the lack of spacing and proper task
identification
They can be edited to make them easier to understand
Scrolling codes
19 /
Best Practice Sharing
Module 5: Macros
5.3. VBA View
Below is a view from the previous Macro EDITED
Titles have been added to identify each operation the Macro
performs step by steps
Scrolling codes have been deleted to lighten the Macro
Overall spacing has been modified to lighten the presentation
20 /
Best Practice Sharing
5.4. Trouble Shooting
21 /
Best Practice Sharing
Module 5: Macros
5.4. Trouble Shooting
5.4.1. Basics
Macros will tell you when they are unable to perform one of the coded operation
Macros automatically stop the first time they are experiencing a problem with one of the coded
operation
Macro error box
Select Debug to see which
operation the Macro was
unable to perform
The exact step where the Macro encountered a
problem
This can give you a hint of what is going wrong
Notes and Observations
Macros will NOT tell you is the result of an operation makes sense if the operation is feasible the
Macro will perform it
Macros dont know a ROI of 1000% doesnt make sense
Macros dont know negative volume doesnt make sense
It is your responsibility to provide the Macro with the proper data so that the operations result in valid
answers
22 /
Best Practice Sharing
Module 5: Macros
5.4. Trouble Shooting
5.4.2. Not All the Files Are Open
Macros operate in Excel files Go to this file, do that, go to this other file and do that, etc.
If a file where an operation needs to be done is not open when you run the Macro you will get an
error message
ALL files that are to be used by a Macro have to be open before running the Macro
5.4.3. Change In File Names
The files that are open prior to running a Macro have to be spelt the same way the files in the codes
are the Macro has to guide itself with names and will only recognize files written as per the
codes
Same logic applies to tab names
Be careful with the file extensions (.xls / .xlsx / .xlsm) a switch from .xls to xlsx is enough make the
Macro not to recognize a file
5.4.4. Change In File Structure
The structure of the templates and files used by a Macro has to stay the same
If the code says that the funded volume has to be pasted in cell A1 of a given file you have to
make sure that A1 corresponds to the funded volume every time you run the Macro
If you have a new template or file, you have to make sure that the areas were values
should be inputted still correspond to the areas in the codes
Maybe funded volume now has to be inputted in cell B2
If the structure of a template or file changes and you fail to adjust the codes the Macro will still
perform the operations, but with the incorrect cells
23 /
Best Practice Sharing
Module 5: Macros
5.4. Trouble Shooting
5.4.5. Sequencing and Calculation
Operations in a Macro have to be sequenced so that operations on which others are coded precede
the dependent operations
To avoid any problems related to manual calculation options in Excel you can enter Calculate
as a code in the VBA view Calculate will have the Excel file to calculate all formulas before
moving on the next operation in the code

S-ar putea să vă placă și