11. Visual Basic Macro Recorder

Post on 22-Feb-2016

36 views 1 download

description

11. Visual Basic Macro Recorder. Open Excel. Click File -> Save As. Save As a Excel Macro-Enabled Workbook. Click File -> Options. Click Customize Ribbon. Tick Developer. Click OK. Select Developer menu. Macro Recorder. - PowerPoint PPT Presentation

Transcript of 11. Visual Basic Macro Recorder

11. Visual BasicMacro Recorder

Open Excel

Click File -> Save As

Save As a Excel Macro-Enabled Workbook

Click File -> Options

Click Customize Ribbon

Tick Developer

Click OK

Select Developer menu

Macro Recorder

• A job may require you to do the same repetitive task every day in Excel

• You can make Excel complete some of these tasks automatically by recording a macro

• All you have to do to repeat the action is to click a button

Formatting Cells

• If you are creating tables of results you may need to format them in a particular way e.g. font, border, background

• Rather than doing each step repeatedly we can record the actions once, and then make Excel repeat them

Type in this table of information

Highlight these cells

Click Record Macro

Macro name: Format TableShortcut key: Ctrl+o

Click OK

Every action we take from now on will be recorded until we click Stop Recording

Right-click with mouse and select Format Cells

Click Font and select Times New Roman,Bold, Size 14 and Colour Dark Blue

Click Border and then the Double Lines

Select Top and Bottom borders

Click OK

Click Stop Recording

Click Visual Basic

Select Module 1

Excel has recorded all of the tasks which you have just completed, and written a program

You can make changes to this program by typing into the script

Click Close

Type in the following table

Highlight the new table

Press Ctrl + oThis is the shortcut key we assigned to our macro

The macro is run and all of the tasks are completed automatically

Challenge

• Go into Visual Basic editor• Change the Font Name to “Verdana”• Change the Font Style to “Italic”• Change the Font Size to 12• Save and Close the Visual Basic editor• Select the table again and press Ctrl+o to

reformat the table

Trial and Error

• The best way to learn specific commands in Visual Basic is to use the Macro Recorder

• You can look at the code that is generated and then try to make some changes

Stock Screener

• By using some creativity you can create complex programs using Visual Basic

• In the following example we will download data from Yahoo Finance, and then filter it to find companies with a low P/E ratio

Select Sheet 2

Click Record Macro

Type Macro name: StockScreenerShortcut key: Ctrl + l

Click OK

Click Data -> Import From Text

Type in the following web addresshttp://finance.yahoo.com/d/quotes.csv?s=MSFT+GOOG&f=snpryv

Click Open

Click Cancel if any passwords are requested

Click Next

The original data is separated by commas so tick Comma

Click Next

Click Finish

Click OK

Click Cancel if any password is requested

Imports the data which we asked for

Select cell A1

Select Insert -> Insert Sheet Rows

Type in the following headings

Select Column D

Click Data -> Filter

Click on Dropdown box, then Number Filters -> Less Than …

Choose PE less than 20 and click OK

Filters data to show companies with a P/E less than 20

Click Developer -> Stop Recording

Click Visual Basic

View the code for the stock screener

We can edit which companies we want to download by changing the ticker symbols

Replace MSFT+GOOG withAXP+DIS+GE+KO+MCD+PFE

Click Save and Close

Select Sheet 3

Press Ctrl+l to run macro with new companies

Shows which companies have a P/E under 20

Challenge• Create a new sheet

• Edit the Visual Basic code to download information for the following tickers:– INTC– JNJ– JPM– KO– XOM

• Change the filter criteria to select companies with a dividend yield less than 3