56
Spreadsheets in Finance and Forecasting Presentation 9 Macros

Spreadsheets in Finance and Forecasting Presentation 9 Macros

Embed Size (px)

Citation preview

Page 1: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Spreadsheets in Finance and Forecasting

Presentation 9Macros

Page 2: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Objectives After studying this week’s material you

will be able to: Use macros to automate formatting and

printing Add controls to your worksheets which

will enable users to : Switch between pages Update sections of a database Add selected charts to illustrate tables.

Page 3: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Using this Presentation The presentation is broken down into an

Introduction and 5 separate sections. You should work through these in order,

making sure that you have completed all the tasks in one section before going on to the next.

A spreadsheet called Macro Examples accompanies this presentation.

When you have finished all the sections in this presentation, you should go on to Formative Activity 7.

Page 4: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Introduction

What are Macros?How do we create them?

Page 5: Spreadsheets in Finance and Forecasting Presentation 9 Macros

What is a Macro? A macro is a piece

of Visual Basic Code which executes a task or series of tasks.

Macros are usually “recordings” of actions performed by the user.

Page 6: Spreadsheets in Finance and Forecasting Presentation 9 Macros

What are Macros for? Macros are used to

save time and effort. For example if you

always format your tables in a particular way, you can use a macro to do this for you to save you pressing lots of buttons and remembering how you did it last time.

Page 7: Spreadsheets in Finance and Forecasting Presentation 9 Macros

How do Macros work? We “record” Macros,

in the same way that you would record an event with a video-recorder.

You set press a “record” button, and Excel records all your actions.

Later on you can “play back” the actions you recorded.

Page 8: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Macro Examples Spreadsheet

The spreadsheet Macro Examples accompanies this presentation.

During this presentation we will develop examples of Macros to carry out the tasks in the following areas:1. Printing2. Formatting3. Page Switching and Selection4. Sorting5. Worksheet Options

Page 9: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Macro Examples The spreadsheet

Macro Examples will be used to develop some of the ideas.

Load this spreadsheet now, so that you can do the activities.

ActionPoint

Page 10: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Macro Toolbar SettingsBefore you do any work with macros you need to ensure that you have Excel configured correctly

Click on View – Toolbars and Select the Visual Basic Toolbar

Click on View – Toolbars and Select the Visual Basic Toolbar

TheVisual Basic Toolbar appears up here

TheVisual Basic Toolbar appears up here

ActionPoint

Page 11: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Recording Macros

The sequence for Recording Macros is:1. START RECORDING:

Click on the Blue Dot (Macro Toolbar)Name the Macro

2. RECORD ACTIONS:Carry out the Action you wish to record

3. STOP RECORDING:Click on the Blue Square (Macro Toolbar)

Page 12: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Testing, Testing …Just to test that your toolbar is working, try the following: Click on the Blue

Square In the dialogue box,

write Testing Click on OK Click on the Blue Circle

ActionPoint

NB. You may find that a separate little toolbar appears, with a Blue STOP square on it.

If you have this, then you can use this instead of the button on the Macro toolbar.

NB. You may find that a separate little toolbar appears, with a Blue STOP square on it.

If you have this, then you can use this instead of the button on the Macro toolbar.

Page 13: Spreadsheets in Finance and Forecasting Presentation 9 Macros

… Testing To see if your macro

has recorded, click on Tools Macro Macros

This dialogue box shows you that you have successfully recorded a macro called Testing

ActionPoint

Page 14: Spreadsheets in Finance and Forecasting Presentation 9 Macros

There’s nothing in it… If you have got this far, then you have

successfully created your first macro. Unfortunately, it’s empty. We did not put any actions in it, so it

doesn’t do anything. However, during the next section we will

create a macro to carry out useful tasks.

Page 15: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Section 1

Macros for Printing

Page 16: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Macros for Printing We will develop

Macros to : Print out a

selection Print out in Portrait

form Print out in

Landscape form

Page 17: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Examples: Printing The page is a set of

tables documenting the number of student enquiries at the registry counter over a 4 week period.

The first Macro we will produce will simply print out a single week’s table

First of all, select the table for week 1 using the mouse.

ActionPoint

Page 18: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Recording a Macro To record the

macro, use the blue button on the Macro Toolbar

Alternatively, Click on Tools Macro Record New Macro

ActionPoint

Page 19: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Macro Recorder This Dialog Box

appears. Change the Macro

Name to PrintWeek

Click on OK

ActionPoint

Page 20: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Recording the PrintWeek Macro

The Macro Recorder button should appear here or in the toolbar.

This means that every action that you do from now on is being recorded.

Page 21: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Print Dialog Box Click on File – Print The Dialog box

(left) appears. We will print the

selection. When you press

OK, the printing will take place.Action

Point

Page 22: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The PrintWeek Macro Now click on the

The square blue Macro Recorder button.

The recording has now stopped, and PrintWeek is now a Macro, and is now a new Excel command.

ActionPoint

Page 23: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Running the Macro Highlight the Week 2

table with your cursor, and then:

Select Tools- Macro-Macros

The dialog box right shows that you have recorded the Macro.

Select this macro, then click on Run.

This will print out the table you have selected.

ActionPoint

Page 24: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Assigning the Macro to a Button

Bring the Forms toolbar onto the screen. (View-Toolbars- Forms)

From the Forms toolbar, select a Button, and put it on the page at around H25-26.

This dialog box appears. Assign the Macro

PrintWeek to the button by selecting it.

Click OKActionPoint

Page 25: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Renaming the Button Right click on the button and Edit

Text. Rename the Button Print Selection.

ActionPoint

Page 26: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Printing another section Now highlight

Week 4, and press the button.

This time week 4’s text is printed.

ActionPoint

Page 27: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Another print button Highlight Week 1

and Week 2. Select Tools-

Macro-Record New Macro

Name the Macro PrintPortrait

Click on OKActionPoint

Page 28: Spreadsheets in Finance and Forecasting Presentation 9 Macros

PrintPortrait Macro Click on File- Page

Setup Select Fit to 1

page wide by 1 page tall

Click on OK

ActionPoint

Page 29: Spreadsheets in Finance and Forecasting Presentation 9 Macros

PrintPortrait Macro Now Click on File-

Print Select Selection Click on OK

Then click on the Blue Button on the Macro Recorder to turn it off.

ActionPoint

Page 30: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Finishing Off Attach the Macro

PrintPortrait to a button. Call this “Portrait”.

Check that it works by selecting weeks 3 and 4, and printing off.

ActionPoint

Page 31: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Printing Challenge Create a button called “Landscape” which

runs a Macro PrintLandscape that prints the selected area in Landscape form.

ActionPoint

Page 32: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Section 2

Macros for Formatting

Page 33: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Macros for Formatting

We will create a macro which uses an on-screen template as a model for the formatting of a number of similar tables.

Page 34: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Formatting

Week 1Students Mon Tue Wed Thu Fri9:00-10:00 27 42 40 4 4410:00-11:00 14 0 41 38 1111:00-12:00 27 17 40 16 2912:00-13:00 41 18 17 33 113:00-14:00 21 48 20 22 315:00-16:00 24 42 12 32 2416:00-17:00 26 28 36 23 32

Switch the Page to Formatting, and move the cursor over to the right, at around column Q

Select and highlight the block of cells Q4 : V12

ActionPoint

Page 35: Spreadsheets in Finance and Forecasting Presentation 9 Macros

UsingAuto Format Click on Format –

Auto format, and this menu box appears.

Scroll down through the examples until you find this one:

Click on OK

ActionPoint

Page 36: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Formatted Text

Week 1Students Mon Tue Wed Thu Fri

9:00-10:00 6 14 16 44 010:00-11:00 28 20 27 34 011:00-12:00 29 28 19 41 1012:00-13:00 45 30 19 7 4113:00-14:00 47 35 5 36 2215:00-16:00 42 28 9 5 3516:00-17:00 44 19 11 44 0

This is the result of the Auto-formatting of the text you selected.

Now scroll the cursor left and position it at cell H2.

ActionPoint

Page 37: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Formatting Macro Click on Tools-Macro-

Record New Macro Call the new Macro

FormatWeeks

Start Recording

Move the cursor to the right and highlight cells Q4:V12

Copy this block of cells

Week 1Students Mon Tue Wed Thu Fri

9:00-10:00 8 10 5 14 3510:00-11:00 17 1 16 8 3611:00-12:00 15 25 31 30 4212:00-13:00 26 28 12 33 3613:00-14:00 45 48 19 44 4415:00-16:00 49 14 25 15 3916:00-17:00 43 24 39 35 37

ActionPoint

Page 38: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Formattingthe tables

Position the cursor over cell B4 and click on Edit-Paste Special – Formats

Click OK Repeat this at cells

B15, I4 and I15. Now click on cell H2

Stop Recording

ActionPoint

Page 39: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Adding a Button Add a button at around cell H6 Assign the Macro FormatWeeks that

you have just recorded. Call the Button Format

ActionPoint

Page 40: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Trying a different Format Now move over to

cells Q4 to V12, and experiment with a different format.

You could try one of the auto formats, devise your own or modify an auto format like this one:

Week 1

Students Mon Tue Wed Thu Fri

9:00-10:00 43 34 16 42 210:00-11:00 44 9 26 36 1211:00-12:00 4 11 0 6 3912:00-13:00 30 13 11 46 013:00-14:00 17 14 32 27 2515:00-16:00 43 10 48 44 2616:00-17:00 23 3 43 31 2

ActionPoint

Page 41: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Formatting all the tables Clicking on the button will now transfer

your formatting over to all the tables

ActionPoint

Page 42: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Section 3

Macros for Page Selection

Page 43: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Page Selection We will create

macros that will Switch back and

forth between two pages

Select pages in a workbook from a Menu

Page 44: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Switching Pages Examine Page 1 and

Page 2 of the Macro Examples Spreadsheet.

On Page 1, record a Macro “Page2” which turns to Page 2.

Create a button on Page 1 called “Page 2”, which, when you press it takes you to Page 2.

ActionPoint

Page 45: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Switching Pages On Page 2 of the

Examples Spreadsheet, create a button called “Page 1”, which, when you press it takes you to Page 1. Action

Point

Page 46: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Options

Page Selection

Macros on this page is designed to illustrate selection of pages.

Go to Page 1

Go to Page 2

On the Page Selection sheet, there are two option buttons.

Right Click on each of these in turn to attach the appropriate Macro Code so that they do exactly what it says on the label!

ActionPoint

Page 47: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Section 4

Macros to Sort Data

Page 48: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Sorting We will create

Macros to sort a table on a particular row or column.

Page 49: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Sorting Macros Switch to the page

called Sorting. Record a Macro

called Sort 1 Highlight the table

as shown here, cells C4 to G10

ActionPoint

Page 50: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Sorting Macros Now click on Data and

Sort. Select Options and

when the second dialogue box appears, select click on Left to Right.

Click Ok, and in the first box, select Row 5.

Click on OK to finish. Now stop recording.

ActionPoint

Page 51: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Sorting Buttons Add 6 separate buttons to the spreadsheet which

will be assigned to macros Sort1, Sort2 etc., and which sort the table according to each of the rows.

Create These

Basket Type Bettabuy ESCOT All-Save DAD'S Cornershop

Single (Economy) £33.25 £30.21 £24.98 £19.03 £29.91

Single (Professional) £42.93 £37.08 £46.74 £44.89 £51.38

Couple (Economy) £37.24 £42.29 £38.95 £40.01 £38.23

Couple (Professional) £71.48 £55.54 £57.98 £69.98 £88.02

Family (Economy) £68.21 £72.75 £74.68 £66.02 £68.01

Family (Professional) £113.23 £117.98 £139.72 £144.20 £168.98

Price Comparison Chart: 5 Supermarkets

Sort 1

Sort 2

Sort 3Sort 4

Sort 5

Sort 6

ActionPoint

Page 52: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Section 5

Macros to change the look of the Worksheet

Page 53: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Worksheet Options The final two

macros will change the look of the worksheet itself.

Page 54: Spreadsheets in Finance and Forecasting Presentation 9 Macros

Two More Macros On the final page

called Blank Page, create two macros which:1.Removes Gridlines2.Removes the Border

Frame You will need to select

Tools-Options, which brings this menu up. Action

Point

Page 55: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Final Two Macros

Hide Gridlines

Hide Number frame

Show Gridlines

Show Number Frame

Finally add two buttons which switch on the number frame and the gridlines.

ActionPoint

Page 56: Spreadsheets in Finance and Forecasting Presentation 9 Macros

The Next Steps Formative Activity 10

takes you through more examples of how macros can be used

These will be developed using the spreadsheet Cornershop Revisited

Macros include: Updating a database Switching within a sheet Creating Charts

SALES Mon Tue Wed Thu Fri Sat SunConfectionery £144.54 £140.15 £146.95 £148.95 £156.98 £201.76 £220.44

Fancy Goods £18.84 £27.70 £38.74 £23.85 £49.48 £70.51 £111.17

Groceries £161.28 £136.30 £105.68 £44.10 £191.12 £246.25 £189.80

Newspapers £143.08 £145.73 £134.63 £136.66 £183.66 £254.72 £402.46

Stationery £33.32 £56.36 £71.22 £71.60 £59.01 £60.77 £92.56

Other £22.83 £20.01 £34.80 £27.50 £23.67 £25.56 £21.88

Dingle: Sales for Today

SALES MonConfectionery £138.66

Fancy Goods £26.83

Groceries £157.44

Newspapers £152.57

Stationery £152.57

Other £34.43

Cornershop Ltd. (Dingle): Sales for the Past Week

Today's Sales