Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010...

20
Microsoft ® Excel ® Macros Mastering © 2015 The Continuing Education Center, Inc. d/b/a National Seminars Training. All rights reserved, including the right to reproduce this material or any part thereof in any manner.

Transcript of Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010...

Page 1: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

Microsoft®

Excel®Macros

Mastering

© 2015 The Continuing Education Center, Inc. d/b/a National Seminars Training. All rights reserved,including the right to reproduce this material or any part thereof in any manner.

Page 2: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

2

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

In this webinar, you will learn:1. Why you’ll need (andwant) to use macros

2. How you can record a macro in a few simple steps

3. How you can storemacros in your ownpersonal workbook

4. How to quickly run amacro with a keystrokeshortcut or icon

5. How to add buttons tothe Quick Access toolbarfor rapid macro access

6. How to create aninteractive macro

7. How to debug a macrowith the step method

Why You Need Macros

• As you work with Excel, you may find yourself repeating the samesequence of commands and actions, for example:

— Updating sales figures on multiple worksheets— Plotting the latest monthly data on a chart— Applying special formats— Locating cells with formulas or cells with values— Reconfiguring a mailing label address layout into a database layout

• With a macro, you can store these sets of procedures and not need to repeat the entire sequence of actions each time you needthem performed.

• You will save a good deal of time and effort and improve accuracy bycreating macros.

Sample Short Macros

1. Apply a special numeric format that has no button equivalent in the Ribbon.

2. Select all cells that contain formulas or functions.

3. Apply light-gray border lines to cells with color backgrounds.

4. Use a special format for displaying values in millions.

5. Print a selected area.

6. Create subtotals for a group of cells.

7. Turn Double Accounting Underlining on or off.

8. Select all blank cells within a selected range.

9. Round cell values on the actual cells instead of writing/copyingformulas and using additional columns.

10. Hide a row if values exceed a certain amount.

Multistep Macros

1. Remove records of employees whose termination dates have passed.

2. Append these records to an archival database.

3. Sort the database by department and name.

4. Add subtotals to create new summary totals by department.

5. Print the list showing the detail and summary together.

6. Collapse the list and print just the summary.

7. Remove the subtotals and sort the full database by employee name.

8. Print the results.

9. Sort the database by date of hire.

10. Print those results.

Page 3: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

3

PARTICIPANT NOTEBOOK

WTEXL051515

How Do You Create a Macro?There are three primary ways to create a macro:

1. Record the various commands and keystrokes that comprise the task(s) youwant to automate. These actions get translated into the coding language VBA — Visual Basic for Applications.

2. Type macro code using VBA.

3. Copy macro code from existing macros or from Help screens.

The recording method is the dominant technique for creating macros, but it doesnot cover all possible actions you want a macro to perform.

You’ll need to type certain macro statements that have no equivalent recordingmethod; structures such as Do Loops, For Statements, and If…Then…Else, must be typed or copied into your macro code.

With some macros, you’ll use a combination of all three techniques.

Steps to Record a MacroMethod One

1. In the Ribbon, click the drop arrowbeneath the Macros button in theMacros group on the View tab.

2. Click Record Macro….

Method Two

• Click the button just to the rightof the word Ready on the leftside of the Status bar at thebottom of the screen.

Page 4: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

4

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

The Record Macro Dialog Box 1. Provide a macro name.a. No spacesb. Cannot begin with a numberc. Use the underscore character or uppercaseletters to simulate word breaks.

d. Limited to 255 characterse. Avoid special characters, except forunderscore, as a word separator.

2. If desired, provide a shortcut key combination. All combinations automatically use the Ctrl key.Hold down the Shift key to create a combination,such as Ctrl+Shift+G as a shortcut. Avoidlowercase letters so as to not replace commonlyused shortcuts such as Ctrl+C for Copy andCtrl+V for Paste.

3. Select a location to store the macro. Store in This Workbook if you intend to use it only in the current workbook. Store in Personal MacroWorkbook if you want it to be available in any workbook.

4. Type a description of what the macro does. It’snot critical, but it’s helpful to you and others.

5. Click OK to begin recording.

Record Your Steps

• Perform all the actions that you want stored in the macro.

• To stop recording:

1. In the Ribbon, click the drop arrow beneath theMacros button in the Macros group on the View tab.

2. Click Stop Recording.

or

Click the button just to the right of the wordReady on the left side of the Status bar at thebottom of the screen.

Steps to record when creating a macro that adds color to all worksheet cells with numbers(not formulas):

1. On the Home tab of the Ribbon, click the Find & Select button in the Editing group.

2. Click Go To Special….

3. Click the button next to Constants.

4. Clear the check boxes for Text, Logicals, and Errors.

5. Click OK.

6. Click the Fill Color drop arrow in the Font groupon the Home tab of the Ribbon.

7. Click a color.

Page 5: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

5

PARTICIPANT NOTEBOOK

WTEXL051515

Some Macros Require Typing VBA CodeRecord a macro to toggle Double AccountingUnderlining on or off. Only two steps can be recorded:

1. Turn Double Accounting Underlining off.

2. Turn Double Accounting Underlining on.

Some Macros Rapidly Perform Lengthy,Complicated TasksA macro to transpose data in mailing label layoutinto Excel database (list) layout

• Although mostly created by recording, this macro,too, requires adding additional code to automate a basic procedure so that it gets performed many times.

• This macro is representative of those kinds ofmacros that handle huge amounts of data; itperforms a repetitive task with amazing speed.

The Personal Macro Workbook• The Personal Macro Workbook does not exist untilthe first time you store a macro there. Excel thencreates this file and stores it in the XLSTARTfolder. Upon a normal exit from Excel, a promptasks you to save this workbook.

• The Personal Macro Workbook, once created, is ahidden workbook that is always open (but usuallyhidden) when Excel is running.

• There is usually no reason to unhide this file.

• Any macro stored here is always available,because this workbook is automatically openedevery time you start Excel.

Five Different Ways to Run a Macro1. From the Macros button on the View tab

2. With a keystroke combination that you previously defined

3. From a new button you placed on the QuickAccess Toolbar

4. From a graphic object, ClipArt, or picture placedon your worksheet

5. After activating the Developer tab in the Ribbon,select the Macros button in the Code group ofthe Developer tab.

• Methods 2 and 3 are ideal for Personal MacroWorkbook macros.

• Method 4 is best for macros designed for aspecific workbook.

Assign a Macro to the Quick Access Toolbar (QAT)1. Right-click the QAT and select Customize QuickAccess Toolbar.

2. Click the arrow to the right of Popular Commandsand click Macros.

3. From the list of macros, click the macro to beassociated with the new button.

4. Click the Add button and then the Modify button.

5. Click one of the 181 buttons in the ModifyButton dialog box.

6. Click OK twice.

Page 6: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

6

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

Run a Macro From a Clip Art Image1. Click the Insert tab in the Ribbon.

2. Click Picture or ClipArt.

3. Browse folders to locate a picture or use the ClipArt dialog box to locate an image.

4. Double-click the desired picture or image.

5. Right-click the image and select Assign Macro.

6. Double-click the name of the macro to be represented by the button.

Page 7: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

7

PARTICIPANT NOTEBOOK

WTEXL051515

The Visual Basic Editor1. Press Alt+F11 to get to the Visual Basic Editor.

2. VBA Project Explorer occupies most of the leftside.

3. The Code window occupies most of the right side.

4. You might also see these windows:

a. Properties — lower left corner

b. Watch — lower right corner

5. Mostly, your focus is on the Code window andthe Project Explorer window.

6. Press Alt+F11 to go back to the Excel worksheet environment.

Visual Basic Code• Macro code has syntax and rules, just like spoken languages.

• Most macros contain code generated byrecording, so you should have some idea how toread the code if you recorded the macro.

• A macro begins and ends with the word Sub.

• The single-quote character and everything to theright of it on a line is a comment. These lines areautomatically green.

• Other lines comprise the executable code of the macro.

Running a Macro in Step ModeTo run a macro step by step:

1. Click in the macro code and press the F8 key.

2. Press F8 to step to the next line of code — thecode line is highlighted in yellow.

3. Press the Reset button if you want to stop themacro at any point.

4. Press F5 and the macro will complete theremaining steps without stopping.

Make a Macro InteractiveTo make a macro more flexible, such as allowing itto prompt for user input, you can type the VBAstatement InputBox into a macro, as in the followingexample that prompts the user to type a sheetname. The macro then activates that sheet andprocesses the data there.

SheetName = InputBox("SheetSelector", "Type sheet Name")Worksheets(SheetName).Activate

Page 8: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

8

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

BooksMicrosoft® Access® 2010 Bible

Microsoft® Excel® 2010 Formulas

Microsoft® PowerPoint® 2010 Bible

Microsoft®Word® 2010 Bible

CD-ROMsMastering Microsoft® Excel® Macros

Microsoft® Office® 2010 (19-CD set)

Microsoft® Office® 2013

Online TrainingExcel 2013

Office 2013 Productivity Collection

Recommended Resources

Page 9: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

9 WTEXL051515

Dear Customer,

You are a valued customer and to say “thank you,” we haveincluded the following as a bonus for you. We believe you’llfind it helpful as a job aid or to further your knowledgebeyond today’s broadcast.

Thank you,

Susan EnyeartDirector, Curriculum and Product Development

BONUSMATERIALS

Page 10: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

10

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

BONUS MATERIALS:

Microsoft® Excel® Keystroke Shortcuts, Function Keys, and Other Productivity Tips

Page 11: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

11

PARTICIPANT NOTEBOOK

WTEXL051515

Entering data

Start a new line in the same cell. Alt+Enter

Create or edit a cell comment. Shift+F2

Fill the selected cell range with the current entry — keep current cell active. Ctrl+Enter

Menus

Undo the last action. Ctrl+Z or Alt+Bksp

Repeat the last command action. Ctrl+Y or F4

Show a shortcut menu. Shift+F10

Make the menu bar active. F10 or Alt

Enter a hyperlink. Ctrl+K

Activate the File Open dialog box. Ctrl+O (the letter O)

Activate the File Save As dialog box. Ctrl+S

Open a new workbook. Ctrl+N

Insert a new worksheet to the left of the current worksheet. Shift+F11

Insert, delete, and copy a selection

Copy the selection. Ctrl+C

Paste the selection. Ctrl+V

Cut the selection. Ctrl+X

Insert new row/column above/left of selected row/column. Ctrl+Plus Sign

Delete currently selected row(s) or column(s). Ctrl+Minus Sign

Page 12: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

12

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

Formatting data

Apply general number format. Ctrl+Shift+~ (tilde)

Apply number format, 2 decimals, commas, minus before negatives. Ctrl+Shift+!

Apply currency format, 2 decimal places (negatives in parentheses). Ctrl+Shift+$

Apply percentage format with no decimal places. Ctrl+Shift+%

Apply exponential number format with two decimal places. Ctrl+Shift+^

Apply time format with AM/PM as in 5:43 PM. Ctrl+Shift+@

Apply date format (d-mm-yy), i.e., 9-Aug-06. Ctrl+Shift+#

Increase indent. Ctrl+Alt+Tab

Decrease indent. Ctrl+Alt+Shift+Tab

Apply the outline border. Ctrl+Shift+&

Remove all borders. Ctrl+Shift+_

Apply or remove bold formatting. Ctrl+B or Ctrl+2

Apply or remove italic formatting. Ctrl+L or Ctrl+3

Apply or remove an underline. Ctrl+U or Ctrl+4

Apply or remove strikethrough formatting. Ctrl+5

Display the Format Style command dialog box. Alt+' (apostrophe)

Display the Format Cells command dialog box. Ctrl+1

Hide rows of the selected range or active cell. Ctrl+9

Unhide rows. Ctrl+Shift+9

Hide columns of the selected range or active cell. Ctrl+0 (zero)

Unhide columns. Ctrl+Shift+0 (zero)

Toggle display of outline numbers and symbols. Ctrl+8

Page 13: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

13

PARTICIPANT NOTEBOOK

WTEXL051515

Move and scroll on a worksheet or workbook

Move to the upper-left corner of the current worksheet. Ctrl+Home

Move the active cell to the lower-right corner of worksheet. Ctrl+End

Move to the next sheet in the workbook. Ctrl+Page Down

Move to the previous sheet in the workbook. Ctrl+Page Up

Move down one screen. Page Down

Move up one screen. Page Up

Move one screen to the right. Alt+Page Down

Move one screen to the left. Alt+Page Up

Move to the edge of data in the current column or row. Ctrl+ Arrow Key

Move between unlocked cells on a protected worksheet. Tab

Move to Column A in the current row. Home

Move to the next workbook or window. Ctrl+F6 or Ctrl+Tab

Display the Print command (File menu). Ctrl+P

Scroll to display the active cell. Ctrl+Backspace

Move to the next pane or previous pane. F6 or Shift+F6

Move to the previous workbook or window.Ctrl+Shift+F6 orCtrl+Shift+Tab

Charting and graphical features

Create a chart on a new sheet based on the selected range. F11

Select the next or previous group of items in a chart. É or Ê

Select the next or previous item within a chart series. È or Ç

Duplicate a selected object (picture, ClipArt, WordArt, etc.). Ctrl+D

Page 14: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

14

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

Move within a selection

Move downward. Enter

Move upward. Shift+Enter

Complete current cell entry, but keep active cell in place. Ctrl+Enter

Move clockwise to the next corner of the selection. Ctrl+Period

Move to the right between nonadjacent selections. Ctrl+Alt+È

Move to the left between nonadjacent selections. Ctrl+Alt+Ç

Select cells, columns, rows, objects, or display features in worksheets

Extend the selection by one cell. Shift+ Arrow Key

Extend the selection to the beginning of the worksheet. Ctrl+Shift+Home

Select the entire column. Ctrl+Space Bar

Select the entire row. Shift+Space Bar

Select the entire worksheet. Ctrl+A

If multiple cells are selected, select only the active cell. Shift+Backspace

Extend the selection down one screen. Shift+Page Down

Extend the selection up one screen. Shift+Page Up

With an object selected, select all objects on a sheet. Ctrl+Shift+Space Bar

Alternate hiding/displaying objects and displaying object placeholders. Ctrl+6

Show or hide the standard toolbar. Ctrl+7

Show or hide outlining symbols and outlining panel. Ctrl+8

Page 15: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

15

PARTICIPANT NOTEBOOK

WTEXL051515

Working in cells or in the formula bar

Insert the AutoSum formula. Alt+= (equal sign)

Enter the current date. Ctrl+; (semicolon)

Enter the current time. Ctrl+Shift+; (semicolon)

Alternate displaying cell values and displaying cell formulas. Ctrl+~ (tilde)

Copy the value from cell above active cell into active cell. Ctrl+Shift+" (quote)

Copy a formula from cell above active cell into active cell. Ctrl+' (apostrophe)

Define a name. Ctrl+F3

Calculate all sheets in all open workbooks. F9

Calculate the active worksheet. Shift+F9

Display AutoComplete list; also used in Data Validation lists. Alt+Down Arrow

Toggle between absolute, relative, and mixed address designation. F4

Select cells with special characteristics

Select only visible cells in the current selection. Alt+; (semicolon)

Select all cells that are directly dependent on the active cell. Ctrl+[

Select all cells in any way dependent on the active cell. Ctrl+Shift+[

Select cells that directly contribute to the active cell’s result. Ctrl+]

Select all cells that contribute to the active cell’s result. Ctrl+Shift+]

Select current region (range enclosed by blank rows and columns) around active cell. Ctrl+Shift+Asterisk

Select all cells with comments. Ctrl+Shift+O (letter O)

Page 16: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

16

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

Editing data

Edit the active cell. F2

Delete text to the end of the edit line. Ctrl+Delete

Move edit cursor one word to left or right. Ctrl+È or Ç

After typing a function name, show function arguments. Ctrl+Shift+A

Move edit cursor to the beginning (left side) of the edit line. Home

Move edit cursor to the end (right side) of the edit line. End

Paste a name into a formula. F3

Enter a formula as an array formula. Ctrl+Shift+Enter

Convert the highlighted portion of a formula to its current value. F9

Windows and dialog boxes

Switch to the next program. Alt+Tab

Show the Windows Start menu. Ctrl+Esc

Close the active workbook window. Ctrl+W or Ctrl+F4

Restore the active workbook window. Ctrl+F5

Switch to the next workbook window. Ctrl+F6

Switch to the previous workbook window. Ctrl+Shift+F6

Minimize the workbook window to an icon. Ctrl+F9

Maximize or restore the workbook window. Ctrl+F10

Exit from Excel. Alt+F4

Page 17: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

17

PARTICIPANT NOTEBOOK

WTEXL051515

Excel Function Keys

Function Keys in Excel

Shift Ctrl Alt Ctrl+Shift

F1 Display Help or theOffice Assistant.

Activates theWhat’s This pointer

Create a chart on anew sheet (same as F11).

F2 Edit the active cell. Enter or edit a cellcomment.

File Save AsCommand (same as F12)

F3 Paste a name into a formula.

Paste a functioninto a formula.

Define a name. Create names fromrow/column labels.

F4 Repeat last action; apply abs/rel status.

Repeat the lastFind Nextcommand.

File Closecommand

File Exit command Edit Find searchbackward from last.

F5 Edit Go Tocommand

Display the Finddialog box.

Restore the window size.

F6 Move to the next pane.

Move to the previous pane.

Move to the nextworkbook window.

Jump to VB editor, if open.

Move to theprevious workbookwindow.

F7 Spell check. Move the window.

F8 Extend a selection. Add to theselection.

Resize the window. Display the Macrodialog box.

F9 Calculate all sheetsin all openworkbooks.

Calculate the activeworksheet.

Minimize theworkbook.

F10 Make the menu bar active.

Display a shortcutmenu.

Maximize orrestore theworkbook window.

F11 Create a chart on anew sheet.

Insert a newworksheet to the left of thecurrent one.

Insert an Excel 4.0macro sheet.

Display the VisualBasic Editor.

F12 File Save Ascommand

File Save command File Opencommand

File Print command

Page 18: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

18

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

Excel Productivity Tips

Other Timesaving Techniques

1. Hold down the Ctrl key as you roll the mouse wheel — zoom in 15% increments.

2. To enter the same data in a selected range — type an entry and press Ctrl+Enter.

3. To move/copy a range, right-drag the selection border — the menu appears when you release the mouse.

4. Align the edges of an object (picture, ClipArt, etc.) with cell boundaries — press Alt as you drag the edge.

5. To paste values from a selected range, right-drag its border and select Copy here as Values Only.

6. Press F5, type an address, and press Enter to jump to any cell.

7. Right-click the sheet navigation arrows to see up to 16 sheet names.

8. Double-click the fill handle to copy data or formula down a column.

9. Double-click a cell edge to move the active cell to the end of data in any direction.

Page 19: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

19

PARTICIPANT NOTEBOOK

WTEXL051515

Macro Questions and Answers

1. What if I forgot to assign a keystrokeshortcut to a macro, want to change anexisting keystroke shortcut, or want toeliminate a keystroke shortcut?

In the Ribbon, click the Macros button in theMacros group on the View tab. In the Macrodialog box, click the name of the macro whosekeystroke shortcut you want to adjust (create,change, or delete), then click the Optionsbutton. In the Macro Options dialog box, makethe desired adjustment.

2. How do I stop a macro while it’s running —particularly if it appears to be going haywireand doing things I don’t want it to be doing?

Press the Esc key.

3. How can I cut down on the number of modules and organize my macros in thePersonal Macro Workbook?

Here are two things you can do:

a. Cut and paste macro code so that macros ofthe same type are in the same module.

b. Right-click on a module that’s empty or nolonger needed and select RemoveModuleN. In response to a dialog box askingif you want to export it, click No.

4. My macros get stored in modules namedModule1, Module2, etc. How can I change the name of a module so that it’s more meaningful?

Take these steps:

a. In the Visual Basic Editor, if the Propertieswindow is not visible, activate it by clickingthe View menu and selecting the Properties window.

b. In the Project window of the Visual Basicwindow, click a module whose name youwant to change; this causes the name toappear in the Properties window to the rightof the word Name.

c. In the Properties window, type a new namefor the module to replace the current name.

5. I’ve got a lot of short lines in a macro. Isthere a way to have two or more lines ofcode put on the same line?

At the end of a line, type a colon (:) followed by a space. Then press the Delete key as manytimes as necessary to bring the code up fromthe line below.

Page 20: Excel Macros - images.ruceci.comimages.ruceci.com/PDFS/WTEL1Book.pdf · Microsoft® Access ® 2010 Bible Microsoft® Excel ... Microsoft®Word ® 2010 Bible CD-ROMs Mastering Microsoft®

20

MASTERING MICROSOFT® EXCEL® MACROS

WTEXL051515

MASTERING MICROSOFT® EXCEL® MACROS

6. Can I split a line of macro code onto twolines if it’s unusually long?

To continue a statement from one line to thenext, break the line by typing a space followed bythe underscore character ( _ ). You cannot break astatement in the middle of a keyword value.

7. What if I want to test some new macro codethat might be faster than what I have beenusing; how can I make macro code linesinoperative?

If you think a line of code in a macro isunneeded, insert a single quote at the beginningof the line (this converts it into a comment) andrun the macro. If the macro runs properly,remove the unneeded line. If the macro fails,remove the single quote and thereby return it toits operative state.

8. I recorded a macro that does a lot ofcopying and pasting of data to manydifferent locations. Do I need to see all thatscreen action every time I run the macro?Doesn’t it slow down the macro?

You can hide all that screen action, andfurthermore, it actually makes the macro runfaster. Enter this line of code at the beginning ofthe macro:

Application.ScreenUpdating = FalseWhen you run the macro, you won’t seeanything happening on the screen until themacro finishes.

9. How do I know if a workbook I’m using hasany macros stored in it?

Jump to the Visual Basic Editor (Alt+F11) andlook in the Project Explorer window on the leftside to see if there are any modules associatedwith the workbook in question. Double-click amodule to view the code in the module.

10. What if I create a macro and store it in thecurrent workbook, but later I decide that Icould use it in other workbooks. Can I moveit to the Personal Macro Workbook?

Yes, you can. In fact, it’s quite easy to copy ormove macros from one workbook to another,even when the Personal Macro Workbook is oneof them. Select the code in question and clickthe Copy or Cut button in the macro toolbar;then activate the destination module by clickingthe destination file name in the Project windowof the Visual Basic Editor. Click the modulewhere you want to paste the data and then clickthe Paste button on the macro toolbar.

11. Where is the Personal Macro Workbook stored?

The Personal Macro Workbook usually does notexist until the first time you store a macro there.Excel then creates this file — PERSONAL.XLS— and stores it in a folder named XLSTART.

The Personal Macro Workbook, once created, isa hidden workbook that is always open (butusually hidden) when Excel is running.

There is usually no reason to unhide this file.

Any macro stored here is always available,because the workbook is automatically openedevery time you start Excel.