Page 746 - Excel 2013 All-in-One For Dummies
P. 746
728 Using the Visual Basic Editor
and the one to replace it with in the Replace With text box, click the Find Next button to locate the first occurrence in the current procedure, module, VBA project, or selected text block (depending on which Search option
you use). After this occurrence is selected in the Code window, you have
it replaced with the replacement text by clicking the Replace button. Excel then locates the next occurrence, which you can then replace by clicking the Replace button or pass over to find the next occurrence by clicking the Find Next button.
Don’t use the Replace All button to replace all the occurrences in your macro unless you’re 100 percent sure that you won’t be globally replacing something that shouldn’t be replaced and possibly screwing up your macro big time. I once typed “selection.font.bold = ture” into the Replace With text box when I intended to enter “selection.font.bold = true” as the replacement text when searching for the property “Selection. Font.Bold = False” in the macro. I then clicked the Replace All button only to discover to my dismay that I introduced this error throughout the code! Of course, I then had to turn around and use the Replace feature to find all the instances of “selection.font.bold = ture” and replace them with “Selection.Font.Bold = true”.
Changing settings for VBA properties
Even when you don’t know anything about programming in VBA (and even if you aim to keep it that way), you can still get the gist of some of the more obvious properties in a macro that change certain settings, such as number format or font attribute, by experimenting with assigning them new values.
In the Company_Name macro shown previously in Figure 2-3, for example, you can tell that the section of VBA commands between the line
With Selection.Font
and the line
End With
contains the procedure for assigning various font attributes for the current cell selection.
Going a step further, you probably can figure out that most of these attributes are being reset by making the attribute equal to a new entry or value, such as
.Name = “Calibri”
or
.Size = 12