Excel –
Macros Lesson 3
From the Tools menu, Customize submenu, Options tab,
select all options except “large icons.”
In this exercise we want to create
a table to store purchasing data and we would like to create a macro to store
the data.
Some of the rows in our
invoice will be blank, so we also need to find a way to automatically delete
rows that contain no data.
We can use the data to
generate management reports.
Data used www.cob.sjsu.edu/BUS91L/Textbook/DeliveryFormv2.xls
DO NOT OPEN THE FILE. Save the file on your desktop. Then open the file.
If the security setting on your browser, or in Excel,
is set to high, you may not be able to download or open the file. You can view those settings on
most browsers from the Tools menu, Internet Options submenu, on the Security
tab. Depending on your user privileges, you may or may
not be able to change those settings. In Excel you can view the settings from
the Tools menu, Macro Submenu, Security submenu. Lower the security levels and then try again.
Background
A customer order will
typically consist of several rows of date. Our macro will have to simultaneously
store several rows of data, matching each row of the Invoice. We will store it
in a table like this:
|
Quantity |
Product |
Price |
Revenue |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Because this design mimics
the layout of the Invoice worksheet, we can write a macro to simply copy and
paste directly from the Invoice worksheet into the data table worksheet. Since the data in the latter two fields is
generated by formulas, we will use “paste special” to convert the formulas into
values.
Here’s what this will look
like
|
Quantity |
Product |
Price |
Revenue |
|
3 |
|
Dollar Amount |
Dollar Amount |
|
1 |
Coffee |
Dollar Amount |
Dollar Amount |
|
2 |
Beer |
Dollar Amount |
Dollar Amount |
|
|
|
Dollar Amount |
Dollar Amount |
|
|
|
Dollar Amount |
Dollar Amount |
Some rows in Columns A and B
will be blank, but the
formulas we copied will still appear in columns C and D. To clean up the table
we need to delete these rows.
We will build a second macro
to handle this task. It’s easier to build and test several small macros then
one large macro.
NOTE: We could create one table
that would only record the quantities and product names. We could use a VLOOKUP
formula to find the price. A formula would generate the revenue totals. It would
look like this:
|
Quantity |
Product |
|
Price |
Revenue |
|
|
|
|
=VLookup() |
=A2*D2 |
|
|
|
|
=VLookup() |
=A3*D3 |
|
|
|
|
=VLookup() |
=A4*D4 |
This design would work if we
always charged the same amount for the items on our menu. But, because prices
are constantly changing, we need to store the price at which the item is sold.
Tasks:
Prepare the data storage area.
Create the Macro
Macro 1 –
copy the information into the Purchase Data table
Explanation: If your table
has no data in it, the first End + Down will put the
cursor at the bottom of the worksheet. But, if your table contains one or more
rows of data, the first End + Down will only put the
cursor into the bottom row of data. The extra End + Down makes sure you
actually reached the bottom of the worksheet.
Your cursor should now
be located directly below the last cell in the table that contains data.
Your new data should
now be displayed in the table.
Macro 2 - Delete the blank rows
Save the file on your
desktop. Use the filename InvoiceWithMacros2
Test the Macros
Macros can be edited
using Visual Basic. This was covered in Macros Lesson 1.
Save the file on your
desktop. Use the filename InvoiceWithMacros2.
That’s it. You’re done! Go here for Macros Lesson 1
and Macros Lesson
2