IT tutorials
 
Office
 

Microsoft Excel 2010 : Macros and UDFs - Understanding How the Macro Recorder Works

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Product Key Free : Microsoft Office 2019 – Serial Number
2/16/2013 5:22:03 PM

This section is about the difference between recording a macro that will run successfully on a new dataset and one that will make you cry in frustration when it fails on a new dataset. You’ll rarely be able to record 100 percent of your macros and have them work on different datasets, but with the tips in the following subsections, you’ll greatly improve your chances.

The macro recorder is very literal, especially with the default settings. For example, if you have cell A1 selected, then begin the macro recorder and use the mouse to select your entire dataset in range A1:B10, this is what will be recorded:

Range(“A1:B10”).Select

If the next time you run the macro the dataset is A1:B20, your macro won’t run on all the rows. There are two things you need to change to make the recorded macro work properly. First, don’t use the mouse when selecting ranges. Second, don’t use the default settings of the macro recorder.

Navigating While Recording

To get the most out of the macro recorder, you should use the keyboard shortcuts to navigate the sheet, not the mouse. The reason is that some of the keyboard shortcuts translate to commands instead of specific cell selections. For example, if you record pressing Ctrl+down-arrow to jump to the last row in a column, you will get

Selection.End(xlDown).Select

If your dataset changes in size the next time you run the macro, the preceding line of code will be much more useful than if you’d recorded the macro by using the mouse.

Relative References in Macro Recording

The second rule to successful macro recording is to know when to turn relative referencing on and off. By default, it is off, which has it uses, but you’ll often want it on.

You can turn Relative Referencing on and off as needed while recording a macro.


When relative referencing is off, the macro records specific cell addresses. Imagine you have a list of addresses similar to Figure 1. Each address is exactly three rows, and a blank row separates each address. To transpose an address to a single row, you would follow these steps:

1.
Start in cell A1.

2.
Press the down-arrow key.

3.
Press Ctrl+X to cut the address.

4.
Press the up-arrow key and then the right-arrow key (to move to cell A2, remember, use the keyboard to navigate).

5.
Press Ctrl+V to paste the address next to the name.

6.
Press the left-arrow key once and the down-arrow key twice to move to the cell containing the city, state and ZIP Code.

7.
Press Ctrl+X to cut the city.

8.
Press the up-arrow key twice and the right-arrow key twice to move to the right of the street cell.

9.
Press Ctrl+V to paste the city.

10.
Press the left-arrow key twice and the down-arrow key once to move to the new blank row just beneath the name.

11.
Hold down the Shift key while pressing the down-arrow key twice to select the three blank cells.

12.
Press Ctrl+- to bring up the Delete dialog.

13.
Press R to select the Entire Row option and then press Enter to accept the command.

Figure 1. Transposing this long list of addresses to multiple columns and rows would make it easier to use them in a mail merge.


If you record these steps with relative referencing off, you will get code that is cell specific, as shown in the text box in Figure 13.5. The results of running this code repeatedly on the data from Figure 1 is shown in Figure 2. A couple of repetitions of the code overwrites the first address line, ruining the record.

Figure 2. Recording the macro with relative referencing off recorded the specific cell address for processing the first address, making the code useless for subsequent addresses.


If this example was your first recorded macro, you might despair, as many before you have, at the uselessness of the recorder. But there is a way to make it work, and that’s to turn on the Use Relative References option found in the Code group on the Developer tab. Performing the same steps with relative referencing on returns the results in Figure 3. As the active (or selected) cell moves down the column, the correct fields are cut and pasted to the proper row and column.

Figure 3. Turn on the relative reference option while recording to record your movements instead of the specific cell addresses.


Avoid the AutoSum Button

If you use the AutoSum button while recording a macro, Excel will record the actual formula entered in the cell in R1C1 notation. It doesn’t record that you wanted it to select the range above or to the left of the formula. It’s just not that flexible. So, instead of using the AutoSum button, manually type in the formula mixing relative and absolute referencing.

For example, if you want to sum G2:G13, the AutoSum function will create the formula =SUM(G2:G13), or in R1C1 notation, =SUM(R[-12]C:R[-1]C). When viewed in R1C1 notation, you see how fixed the formula is. Although it will work in any column that it’s placed in, it specifically includes the cells 12 cells above (row 2) and directly above (row 13) the formula cell. The problem is if you add more rows, then the first cell is no longer 12 rows above—it’s more. The solution is to type in the formula manually, fixing the row for the first argument, as shown in Figure 4.

Figure 4. Instead of using the AutoSum button, type in SUM formulas manually, making sure to fix the row of the first argument.

 
Others
 
- Microsoft Excel 2010 : Macros and UDFs - Enabling VBA Security
- Microsoft Word 2010 : Working with Styles (part 3) - Deleting Styles, Revealing Formatting
- Microsoft Word 2010 : Working with Styles (part 2) - Creating Customized Styles, Managing Styles
- Microsoft Word 2010 : Working with Styles (part 1) - Using Quick Styles, Working in the Styles Task Pane
- Microsoft Access 2010 : Viewing the Design of a Form, The AutoForm Feature
- Microsoft Access 2010 : Sorting Records, Filtering the Data Underlying a Form
- Microsoft OneNote 2010 : Taking Notes (part 2) - Creating More Writing Space in the Middle of a Page, Creating More Writing Space at the End of a Page
- Microsoft OneNote 2010 : Taking Notes (part 1)
- Microsoft Visio 2010 : Printing Scenarios (part 4) - Printing High-Quality Materials
- Microsoft Visio 2010 : Printing Scenarios (part 3) - Printing Multiple Drawings on a Single Sheet, Printing Scaled Drawings at Reduced Size
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us