TECHNOLOGY EXCE L - Strategic Finance · 2012-10-01 · VBA Code When creating macros, the...
Transcript of TECHNOLOGY EXCE L - Strategic Finance · 2012-10-01 · VBA Code When creating macros, the...
Let’s say that each month we receive a
travel expense data feed similar to Figure
1. Column A shows the description, and
column B contains the amount. Our task
is to assign an expense type, such as
“Air,” “Lodging,” etc., in column C. As
the number of items grows, this would
quickly become a tedious, laborious task.
Rather than going through each item
individually and assigning a value, we
can create a macro that will take a first
pass through the data and use wildcards
to look for records that are clearly a cer-
tain type of expense. This will leave few-
er entries for us to categorize. Over time,
the macro can be adjusted for new wild-
card searches, thus improving its perfor-
mance and efficiency.
The first step is to create the macro.
First save the workbook with any file
extension except .XLSX. Open the VBA
editor—either type ALT+F11 or go to the
Developer tab and click on Visual Basic.
In the VBA editor, select Insert, Module
to create a blank programming window.
Type the following lines in the module:
Sub TravelMacro()
For Each cell In Application. Intersect
(Range(“A:A”), ActiveSheet.Used
Range)
Application.StatusBar = “Do not
touch. Processing row “ &
cell.Row
If cell.Value Like “*Air*” Then
cell.Offset(0, 2).Value = “Air”
ElseIf cell.Value Like “*Amtrak*”
Then cell.Offset(0, 2).Value =
“Train”
ElseIf cell.Value Like “*Hotel*”
Then cell.Offset(0, 2).Value =
“Lodging”
End If
Next cell
Application.StatusBar = False
Msgbox “Program complete!”
End Sub
The second line in the code (beginning
“For Each cell”) creates a loop that cycles
through each cell in the used range of
column A. Line 3 (Application Status
Bar...) makes the Status Bar display a
message at the bottom of the Excel
screen to show you that the macro is
processing. And the last three lines of the
program clear the message in the status
bar and display a message indicating that
the macro is completed.
Lines 4 (If cell.Value…) through 11
(End If) are the lines that actually per-
form the main task in the macro. For
example, line 4 searches for the word
“Air” in column A. If it’s found, “Air”
is added to column C of the same row.
These lines would need to be changed
if the macro is deployed for other
tasks.
You’ll find some records don’t get
marked with an expense type. Over time,
you might notice other indicators in the
travel description to seach for. You can
update the macro to search for these by
adding more lines after line 11. For
example, you can search for specific air-
lines or hotels names:
ElseIf cell.Value Like “*SWA*”
Then cell.Offset(0, 2).Value =
“Air”
ElseIf cell.Value Like “*Marriott*”
Then cell.Offset(0, 2).Value =
“Lodging”
It takes 60 seconds to add a new pair
of lines, but it’s well worth the effort if it
saves you from typing “Lodging” 100
times every month. Continue adding
new pairs of lines before the End If
statement as you refine the search.
Some of the rows in Figure 1 are
blank. If you want to hide them, add
TECHNOLOGY
EXCELUsing a Macro to Minimize Processing Times
By Mo Ali, CMA, CPA, PMP
54 S T R AT E G IC F I N A N C E I O c t o b e r 2 0 1 2
these two lines before the End If line:
ElseIf cell.Value = “” Then
cell.EntireRow.Hidden = True
Once the code has been entered into
the editor, create a Macro button in the
Excel sheet and link it to the new macro.
To do this, toggle back to the Excel
sheet. Select cell D1. Insert a shape in
the cell. Right-click the shape and
choose “Edit Text.” Label the shape as
“Run Macro.” Right-click the shape
again, select “Assign Macro,” and
choose the new macro. Figure 2 shows
the final result.
VBA CodeWhen creating macros, the easi-
est way to generate the code is
to simply perform the needed
task in a separate Excel work-
book while using the Macro
Recorder. Then you can copy
the VBA lines from that into the
new macro. This works best for
simple tasks such as formatting
text and typing data and is the
quickest way for beginners to
understand the basics of a VBA
program. The code generated
by the Macro Recorder is usually more
than what is needed, but it can be
scrubbed and used for simple tasks.
For more complex tasks where you
can’t record your actions to create the
code, an Internet search is the best place
to begin. For example, if you want to
create a message box, a search for
something like “Excel VBA code for mes-
sage boxes” will result in a range of free
code offerings. It takes a bit of experi-
ence to select the most suitable code to
use, but there are numerous message
boards where you can find willing volun-
teers to answer specific questions about
VBA code.
The overall trick for creating macros is
to properly assemble the code from the
Macro Recorder and other sources in a
program and apply it to the situation
rather than writing code from scratch.
Always test programs with data prior to
deployment to ensure they work correctly.
An Excel VBA class can be helpful, but it
usually isn’t enough by itself. For users
who want to create advanced programs
in Excel, a detailed Visual Basic class is a
better investment. SF
Mo Ali, CMA, CPA, PMP, is an executive
consultant at Morgan Clarke Enterprises
in New York City. He is also a member of
the Princeton Chapter of IMA®. You can
contact Mo at [email protected].
O c t o b e r 2 0 1 2 I S T R AT E G IC F I N A N C E 55
Figure 1
Figure 2