Tip Dont Select Anything

Nothing screams "recorded code" more than having code that selects things before acting upon them. This makes sense—in the Excel interface, you have to select Row 1 before you can make it bold.

However, in VBA we rarely have to do this. (There are a couple buggy features in charts where you do have to select the chart object in order to make the method work.) It is possible to directly turn on Bold font to Row 1 without selecting it. The following two lines of code turn into one line.

Macro recorder code before being streamlined:

Rows("1:1").Select Selection.Font.Bold = True

After streamlining the recorded code: Rows("1:1").Font.Bold = True

There are several advantages to this method. First, there will be half as many lines of code in your program. Second, the program will run faster.

After recording code, I literally highlight from before the word .Select at the end of one line all the way to the dot after the word Selection on the next line and press Delete (see Figures 2.43 and 2.44).

Figure 2.43

Select from here to here...

Hange ("E14 ¡Gl1!") .Select Rons ("1:1"| .EgHBl ffffH^TTTWFnnr. .Fin 1 rl = True Rous("14;14").Select

Figure 2.44

...and hit the Delete key. This is basic "101"of cleaning up recorded macros.

Range("E14:G14").Select

Rows("1:lw).Tone.Bold = True Rows ("14: 14") .Select

0 0

Post a comment