Recording and Modifying a Basic Macro

Recording and Modifying a Basic Macro

A basic macro is a recording of a sequence of commands and typed entries that you can then replay elsewhere in the worksheet. Macros enable you to perform the same sequences of commands in different places, cutting down the time it takes to complete redundant work.

There are ways to automate the formatting of cells that are actually easier than recording and play- ing back macros. So the types of steps you want to record are the repetitious kind that you would otherwise have to repeat yourself dozens of times or more.

Take Note Excel records only those steps that have a direct impact on the contents of the worksheet. To be accurate, it records the impact those steps have, not actually the commands that led to the impact. For example, if you select several rows and columns, Excel records the act of the rows and columns being grouped. But if you expand or collapse that group, it does not record that action because doing so does not impact the worksheet itself. Exceptions include filtering and sorting ranges and tables, which Excel does record.

STEP BY STEP

Record and Modify a Basic Macro

GET READY. LAUNCH Excel 2016 if it is not already open.

1. OPEN the

02 Revenues.xlsx workbook file for this exercise. If the Developer tab is

already displayed on the ribbon, skip to step 5.

2. Click the File tab and then click Options .

3. In the Excel Options dialog box, click Customize Ribbon .

4. In the Main Tabs list on the right, select the Developer check box. This adds the Developer tab to the Excel ribbon, enabling you to more easily record macros. Click OK .

5. The macro that you record creates a custom subtotal row at the place you define, rather than at some place Excel determines. The rule you follow is that the user (you) must select the cell where you want the subtotal to appear, and then run the macro. So to prepare for macro recording, click cell D20 .

6. Click the Developer tab and then in the Code group, find Use Relative References . If it is not highlighted, click to select it. You want relative references for this macro.

7. In the Code group, click Record Macro .

8. In the Record Macro dialog box, click the Macro name box and replace the existing text with CustomSubtotals .

9. In the Shortcut key box beside Ctrl+, type the capital S . This changes the shortcut key to Ctrl+Shift+S. Leave Store macro in set to This Workbook. In the Description box, type Creates custom subtotal rows . The Record Macro dialog box should now appear as shown in Figure 2-21.

10. Click OK . You are now recording a macro.

Figure 2-21 The Record Macro dialog box

50 Lesson 2

Troubleshooting If you mess up a step during the macro recording, don’t worry. Click Stop Recording in the Code group of the Developer tab. Then start again from step 5. Use the same name, and when

Excel asks whether you want to overwrite the existing macro with the same name, respond with Yes.

11. Press Shift+Down Arrow .

12. Click the Home tab and then in the Cells group, click the Insert arrow . In the menu, click Insert Sheet Rows .

13. Press Shift+Up Arrow .

14. In the Editing group, click AutoSum . Do not press Enter.

15. In the Clipboard group, click Copy .

16. Press Tab .

17. Type the partial formula =max( .

18. In the Clipboard group, click Paste .

19. Type ) (end parenthesis) and then press Tab .

20. Press Left Arrow .

21. Click the Font Color arrow , and select Dark Blue, Text 2 (first row, fourth column under Theme Colors).

22. Click the Italic button.

23. Click the Developer tab and then in the Code group, click Stop Recording . As Figure 2-22 shows, the macro generates a total for the bottom of the arbitrary

cluster of records, and also tabulates the highest (maximum) value in that cluster in the cell adjacent to the subtotal.

Figure 2-22 Custom subtotals generated

by the macro

24. On the Developer tab, in the Code group, click Visual Basic . You will make a simple edit to the macro code that doesn’t require knowledge of VBA.

25. In the left pane of the Microsoft Visual Basic for Applications window, expand the Modules folder, if necessary (click the + sign). Then, double-click Module1 . The macro code displays in the Code window on the right.

26. In the Code window, locate the code line that reads “Selection.Font.Italic = True” near the end of the code listing. Drag over the text Italic and type Bold in its place.

51 Be careful not to make any other changes to the code. Your screen should appear

Applying Custom Formatting and Layouts

similar to Figure 2-23.

Figure 2-23 Modifying the CustomSubtotals

macro in the Visual Basic for Applications window

27. In the menu bar and the top of the window, click File and then select Close and Return

to Microsoft Excel .

28. In the worksheet, delete rows 20 and 21 . You will test the macro using the assigned shortcut key and ensure that the macro modification works correctly.

29. Select cell D20 and then press Ctrl+Shift+S . The macro runs and the result in cell E20 appears in bold rather than italic formatting.

30. Click the File tab and then click Save As . Select a folder or click Browse to navigate to the folder where you are saving your files.

31. In the Save As dialog box, under Save as type, choose Excel Macro-Enabled Workbook

(*.xlsm) . Save the workbook as

02 Revenues Solution.xlsm . Click OK if you see a

message box displayed after you save the file. PAUSE. LEAVE the workbook open for the next exercise.

Take Note For security reasons, Excel no longer saves macros in its regular .XLS and .XLSX file formats. Now, the only way to save a macro-enabled workbook is to give it the special .XLSM file type. This way, companies that want to avoid any possibility of spreading malware can enforce policies preventing .XLSM files from being attached to or received within e-mails.

Because of the proliferation of malicious software, Microsoft has set up Excel so that after it’s installed, you cannot execute macros from a file you open (even an explicitly macro-enabled work- book) until you read the notification and click Enable Content. If you never plan to run macros or if you’re skeptical about your office colleagues, you can turn off macros completely. You can also turn off the notifications and enable all macros, if you work in an office such as a financial services provider where macros are in use constantly, you trust the source of the Excel workbooks, and notifications would only get in the way.

52 Lesson 2

STEP BY STEP

Manage Macro Security

GET READY. LAUNCH Excel 2016 if it is not already open.

1. OPEN the

02 Revenues Solution.xlsx workbook from the previous exercise, if you

closed it. Or, you can open a blank workbook.

2. On the Developer tab, in the Code group, click Macro Security .

3. In the Trust Center dialog box, click Disable all macros with notification to have Excel warn you whenever an opened workbook contains macros, enabling you to turn those macros on or off based on your decision. This is the recommended setting for most situations (see Figure 2-24).

Figure 2-24 The Trust Center dialog box

4. Click OK .

5. SAVE the workbook and CLOSE it. PAUSE . LEAVE Excel open to use in the next exercise.

Inserting Form Controls If you are developing a workbook that other people are going to use—either for data input or to

view a report—you might find it helpful to your workbook’s users if you can add controls such as check boxes, command buttons, and spin buttons. Adding controls to a workbook helps to limit a user’s choices or to make the available choices easier to select.

Excel offers two families of controls: Form controls and ActiveX controls. Form controls are na- tive to Excel and are backward compatible to Excel 95. ActiveX controls are used in a variety of applications besides Excel, and can be controlled via VBA. You can add VBA to your workbook to respond to events. ActiveX controls also have more options or properties you can control.

Take Note Try using a Form control first. If you find that you need a property that the Form control cannot offer, try the ActiveX version. Note that many of the ActiveX controls you see on the Developer tab’s Insert menu are from the MS Forms library. That library is external to Excel and must be loaded. This is one of the fundamental differences between the Form controls, which are built in to Excel, and any ActiveX controls.

53 Both types of controls offer the ability to link input (populate a list) and output (show what was

Applying Custom Formatting and Layouts

selected) between the control and a worksheet cell. Note that both Form controls and ActiveX controls float on the worksheet. This means that like

shapes, you can tell Excel whether you want to move and/or resize your controls as you insert, delete, or resize rows and columns.

A basic listing of the available form controls includes the following:

Control Forms

3 3 Used to run a macro in the workbook. Command Button

Check Box

3 3 TRUE/FALSE

Combo Box

3 3 Used to select an option from a list; uses a drop-down button to hide the list until the button is clicked. Note that with an ActiveX combo box, you can pick from the list or type in any value you want. With a Form control, you can only pick from the list.

Group Box

3 Allows you to group Form controls. Most often used to group option buttons in cases where you have more than one group of option buttons on the worksheet.

Image

3 Image controls are usually used for fairly sophisticated solutions that involve VBA. If all you need is a picture, you can simply insert a picture from the Insert tab. You don’t need an ActiveX image control. Given that you can assign a macro to a picture object, ActiveX picture controls tend to

be the best option in a fairly narrow and sophisticated solution band.

Label

3 3 Just a label - you don’t input any values with it. List Box

3 3 Similar to a combo box but shows several options at once. In the case of the ActiveX control, you cannot type in a value that is not on the list (which you can do with a combo box).

Option Button

3 3 Used to pick from mutually exclusive options. If you pick Option A, then Options B and C are deselected (changed to FALSE).

Scroll Bar

3 3 Similar to a spin button, except you have a slider control that allows users to make large changes to the control’s value very quickly.

Spin Button

3 3 Allows you to increment/decrement a number. Text Box

3 Type in any value. Not available as a Form control. Toggle Button

3 TRUE/FALSE—similar to a check box, but has a Triple State option that allows a third #N/A option.

54 Lesson 2

STEP BY STEP

Insert Form Controls

GET READY. LAUNCH Excel 2016 if it is not already open.

1. OPEN the

02 Controls.xlsx workbook file for this exercise.

2. SAVE the workbook as

02 Controls Solution.xlsx .

3. Click the Developer tab. In the Controls group, click the Insert button. Select the ActiveX Combo Box control.

4. Place the mouse pointer close to the top left corner of cell E4 . Before you click the mouse, press the Alt key. Then click the mouse and drag the pointer just above and to the left of the bottom right corner of cell E4 . Release the mouse button first, then

release the Alt key.

5. Click the Combo Box control you just inserted into E4 to select it, if necessary. If you do not see the six circular resizing handles around the borders of the control, then click the Design Mode button on the Developer tab.

6. Next to the Design Mode button on the ribbon is the Properties button. Click the Properties button. The Properties window opens.

7. For the LinkedCell property, in the second column, type G4 .

8. For the ListFillRange property, type B3:B21 .

9. Change the ListRows property to 12 . Compare your settings in the Properties window

with Figure 2-25.

Figure 2-25 Modifying properties for a

combo box ActiveX control

10. Close the Properties window and then click any cell on the worksheet.

11. Click the Design Mode toggle button to turn off Design Mode. Click the drop-down arrow in E4 and select various values. The value of G4 should match your selection.

12. Try typing Taupe into the control and then press Enter . Excel should accept the entry and display Taupe in cell G4.

13. Select cells B3:B21 . In the Name Box on the left end of the formula bar, enter the name lst.Colors and then press Enter (note that the first character in the name is a lowercase “L” not the number 1).

14. Use the process in step 13 to assign the following names to the indicated cells below (again, note that the first character in each name is a lowercase “L” not the number 1):

55 K6

Applying Custom Formatting and Layouts

lnk.Checkbox.F G6 lnk.Checkbox.X

K4 lnk.ComboBox.F K8

lnk.Listbox.F G11

lnk.Listbox.X G23

lnk.Option1.X G25

lnk.Option2.X G27

lnk.Option3.X K20

lnk.Option.F G8 lnk.Textbox.X

15. On the Developer tab, in the Controls group, click Insert and then select the ActiveX

Check Box control.

16. Place the mouse pointer close to the top left corner of cell E6 . Before you click the

mouse, press the Alt key. Then click the mouse and drag the pointer just above and to

the left of the bottom right corner of cell E6 . Release the mouse button first and then release the Alt key.

17. Click the Check Box control you just inserted into E6 to select it, if necessary. If you do not see the six white circular resizing handles around the borders of the control, then click the Design Mode button on the Developer tab.

18. Click the Properties button.

For the Caption property, type Matte Finish? For the LinkedCell property, type lnk.Checkbox.X

19. Turn off Design Mode and test the check box control.

20. Repeat previous instructions as appropriate to add the following controls:

An ActiveX Text Box control in E8 with a LinkedCell property of lnk.Textbox.X

An ActiveX List Box control in E11:E21 , LinkedCell property is lnk.Listbox.X and the

ListFillRange is lst.Colors

An ActiveX Option Button control in E23 , LinkedCell property is lnk.Option1.X and the

Caption is Matte Finish

An ActiveX Option Button control in E25 , LinkedCell property is lnk.Option2.X and the

Caption is Glossy Finish

An ActiveX Option Button control in E27 , LinkedCell property is lnk.Option3.X and the

Caption is No Finish

21. Close the Properties window, turn off Design Mode, and test your controls.

22. On the Developer tab, click the Insert button. This time insert a Form control Combo

Box into cell I4 .

23. Right-click the combo box control and select Format Control from the shortcut menu. Select the Control tab.

24. Set the Input range to lst.Colors and the Cell link to lnk.ComboBox.F and then click OK .

25. Click any regular cell. If the pointer changes into a pointing finger when you mouse over the control in cell I4, then you can use it.

26. Try typing Taupe into the Forms combo box in cell I4. Unlike the ActiveX combo box, you can’t type into a Forms combo box.

27. Place a Form control check box in I6 .

28. Right-click the check box and select Format Control .

29. On the Control tab, set the Cell link to lnk.Checkbox.F .

30. Click the Alt Text tab and change the alternative text to Matte Finish? Click OK .

31. Click in cell H6 then press the right-arrow key once. Then press Delete . This will clear out the text in I6 so you can read the check box better.

56 Lesson 2

32. Notice that changing the Alt Text in step 30 didn’t change the check box’s caption.

Right-click the check box control and select Edit Text to change the caption to Matte

Finish? .

33. Place a Form control list box across cells I8:I18 . (Remember, if you press the Alt key prior to starting your drag, the control will snap to the cell borders.)

34. Right-click the list box and select Format Control from the shortcut menu. Make the following entries:

Dokumen yang terkait

Analisis Komparasi Internet Financial Local Government Reporting Pada Website Resmi Kabupaten dan Kota di Jawa Timur The Comparison Analysis of Internet Financial Local Government Reporting on Official Website of Regency and City in East Java

19 819 7

Analisis Komposisi Struktur Modal Pada PT Bank Syariah Mandiri (The Analysis of Capital Structure Composition at PT Bank Syariah Mandiri)

23 288 6

Improving the Eighth Year Students' Tense Achievement and Active Participation by Giving Positive Reinforcement at SMPN 1 Silo in the 2013/2014 Academic Year

7 202 3

An Analysis of illocutionary acts in Sherlock Holmes movie

27 148 96

Improping student's reading comprehension of descriptive text through textual teaching and learning (CTL)

8 140 133

Teaching speaking through the role play (an experiment study at the second grade of MTS al-Sa'adah Pd. Aren)

6 122 55

Enriching students vocabulary by using word cards ( a classroom action research at second grade of marketing program class XI.2 SMK Nusantara, Ciputat South Tangerang

12 142 101

The Effectiveness of Computer-Assisted Language Learning in Teaching Past Tense to the Tenth Grade Students of SMAN 5 Tangerang Selatan

4 116 138

Analysis On Students'Structure Competence In Complex Sentences : A Case Study at 2nd Year class of SMU TRIGUNA

8 98 53

The correlation between listening skill and pronunciation accuracy : a case study in the firt year of smk vocation higt school pupita bangsa ciputat school year 2005-2006

9 128 37