You are on page 1of 5

Lab 2

ES2513
The Macro Record Feature & VBE

Purpose of the assignment is to learn how to use the VBA macro record.
I. The Macro Record Feature, refer to chapter 2 in the course text (T. Urtis).
1. Make sure your system is set up to show the file extensions.
a. Click start button (lower left corner)
b. Select the Control Panel
c. Select the Appearance and Personalization
d. Click Folder Options
e. Select the View tab and from Advanced Settings,
f. Look for the Hide extensions for known file types selection box. De-select
the box.
2. Add the VBA Developer to the ribbon interface:
a. Select the File tab within the ribbon.
b. Select Options from the File dropdown menu
c. Select Customize Ribbon on the left.
d. From the selection pane labeled Customize Ribbon, look for the selection
box labeled Developer. Make sure the Developer box is selected.
e. Select OK to complete.
3. Give your workbook and worksheet descriptive names. Save it as a Macro enabled
workbook: *.xlsm. If you dont save it now, you will have regrets later.
4. You will create your first macro using the record button. This is similar to lesson
two in the textbook (and accompanying DVD), by T. Urtis.
5. You will work on two worksheets for this lab. After completing each macro, go to the
second worksheet and run it to confirm it works correctly.

Recording Your First Macros (RaceTable1)


1. Place the mouse pointer in cell A1. Click the mouse once to activate the cell.
2. Select the Developer tab. Press the macro record button in the ribbon. A panel will
appear where you type the name of your macro and enter a message describing
what the macro will do. Name your macro RaceTable1. Select OK, when you have
filled in the fields.

3. Select cell C6. Create a table on a worksheet that contains a header with fields for
Finish Place, Racer Number, Racer Name, and Lap Number.

Finish Place
Racer Name
Racer Number
Lap Number
4. Below the Lap Number, enter a column of values from 1 to 25. This represents the
25 laps for the race.
5. Go ahead and resize the cells and center the text.
6. Select the A1 cell and select the Stop Recording icon. This will end the recording.
7. Go to the 2nd worksheet and test the macro.
8. Return to the first worksheet, and start the next macro.
Next Macro (RaceTable2)
1. Select the A1 cell. Start the Macro Recorder again. Name your macro RaceTable2.
2. Next to the lap-number, fill in the Lap Time and Cumulative Time. Copy these two
cells horizontally for six drivers. The example below shows the pattern for the first
two drivers. Resize the cells and center as desired.

Finish Place
Racer Name
Racer Number
Lab Number

Lap Times

Cumulative
Time

Lap Times

Cumulative
Time

3. Merge two horizontal cells just to the right of the Finish Place cell, copy that merged
pair to all the cells above the Lap Times and Cumulative times for all six racers. The
table should look similar to this example, but for all six racers.

Finish Place
Racer Name
Racer Number
Lab Number

Lap Times

Cumulative
Times

Lap Times

Cumulative
Times

4. Select the A1 cell and select the Stop Recording icon. This will end the recording.
5. Go to the second worksheet and test the macro.
6. Return to the first worksheet, and continue

Yet Another Macro (RaceTable3)


1. Select the A1 cell. Press the Macro Record button in the ribbon. Name your macro
RaceTable3. Select OK, when you have filled in the fields.
2. Lap times for the racers will be computed using a random number generator:
=TIMEVALUE( "02" & ":" & TEXT(10*RAND(), "0.0") )
Type (dont copy and paste) the above formula into the D10 cell (this is the cell
corresponding to the first lap time for the first driver).
3. Copy the D10 cell into all the other Lap Times cells (150 total cells). Do not copy
into the cells designated for the cumulative times.
4. The cumulative time for lap-n is the previous cumulative time (for lap n-1) plus the
time for lap-n. Fill-in the cumulative times for all drivers.
5. Single click the A1 cell to activate that cell, and Stop Recording.
6. Test your macro on the second worksheet.
7. Return to the first worksheet.

Set the Race Data (RaceTable4)


1. Place the mouse in the A1 cell and click to activate that cell.
2. Press the Macro Record button in the ribbon. Name your macro RaceTable4. This
macro will fix the data so it will not change.
3. Select all the cells containing the lap times, and the Cumulative times. Select the
Format Cells option. Select Time from the Category list. From the Type: selection
box, choose the xx:xx.x option. All your lap times should be formatted as 02:xx.x.
4. Select all times using your mouse and right click. Select Copy (you can use the
keyboard shortcut).
5. Right click on the field of selected lap times and click Paste Special. From the
Paste Special panel, select the Values radio button beneath the Paste-header in the
panel. This will replace the formula for generating the random lap times with the
current cell values. Select the OK-button to complete the operation.
6. Single click the A1 cell and Stop Recording.
7. Test your macro on the second worksheet
8. Return to the first worksheet.

Race Results (RaceTable5)


1. Create a new macro to do the following for each racer (you must research how to
use each function before creating your macro with the macro recorder):
a. Place the fastest lap time for each racer at the bottom of the Lap Times
column. Label the row on the left of the table. Use the MIN function.
b. Place the slowest lap time for each racer (MAX function) beneath the fastest
lap time. Label the row.
c. Place the average lap time for each racer beneath the slowest lap time. Label
the row.
d. Use the RANK function to determine the order of the finishers at the top of
the table.
e. The racer numbers can be generated randomly using this expression:
=FIXED( 100*RAND(), 0 )
f. Test your macro.

Borders, Cell Ruling & Color (RaceTable6)


1. Create another macro to rule lines for the cells and make a border for the data in
your table.
2. Add color to improve the readability of the table.
3. Test your macro.

Test Run
1. Create or move to a third worksheet and run ALL your macros to generate the table.
2. Enter names for the racers. The names should be your full name, the lab
instructors name, Kwame Rhodes, Helen Wheels, Stormy Driver, and Wanda
Steers. You must use these names only. Enter them in any finish order you desire.
3. Name this worksheet TestRun

The VBE, Visual Basic Editor


1.
2.
3.
4.

Choose the Editor icon to activate the VBA editor.


Rename the Module1 to something like MyRecordedMacros.
From the Insert drop down menu, choose Module to create a new module.
Create a new macro using the VBE. Enter the name of each recorded macro into this
macro as follows:
Sub runRecordedMacros()
Call RaceTable1
Call RaceTable2
etcetera until all the recorded macros are entered in this macro file.
End Sub

5.
6.
7.
8.

Rename the module to describe what it does.


Create a new worksheet. Test this new macro and verify it works correctly.
Name this worksheet FinalRun
Delete all but the TestRun and FinalRun worksheets.

You might also like