Advertisement
U.S. markets open in 6 hours 53 minutes
  • S&P Futures

    5,208.75
    -6.00 (-0.12%)
     
  • Dow Futures

    39,211.00
    -12.00 (-0.03%)
     
  • Nasdaq Futures

    18,187.25
    -44.25 (-0.24%)
     
  • Russell 2000 Futures

    2,048.30
    -1.50 (-0.07%)
     
  • Crude Oil

    82.55
    -0.17 (-0.21%)
     
  • Gold

    2,159.00
    -5.30 (-0.24%)
     
  • Silver

    25.11
    -0.15 (-0.59%)
     
  • EUR/USD

    1.0870
    -0.0007 (-0.07%)
     
  • 10-Yr Bond

    4.3400
    0.0000 (0.00%)
     
  • Vix

    14.33
    -0.08 (-0.56%)
     
  • GBP/USD

    1.2708
    -0.0020 (-0.16%)
     
  • USD/JPY

    150.2960
    +1.1980 (+0.80%)
     
  • Bitcoin USD

    64,821.93
    -3,570.75 (-5.22%)
     
  • CMC Crypto 200

    885.54
    0.00 (0.00%)
     
  • FTSE 100

    7,722.55
    -4.87 (-0.06%)
     
  • Nikkei 225

    40,003.60
    +263.20 (+0.66%)
     

Pogue's Basics: How to create a search-and-replace macro in Word

Over the years, Microsoft has evolved from a word processor into a database, Web design program, and floor wax. Among its thousands of features, macros are power-user tools that could benefit a lot of people.

They allow you to automate tasks you do often, by teaching Word to follow your example. You hit Record Macro, you do something — a search and replace, let’s say — and then you can play back that macro later.

Actually, search and replace is a bad example — Microsoft Word cannot, in fact, record and play back a search/replace. It just records nothing.

That’s a bummer. You know those people who put two spaces after a period? When one of those documents comes my way, I’d love to be able to search for two spaces and replace them with one.

Fortunately, you can create such a macro manually.

Start by choosing Tools -> Macro -> Record Macro. Name your macro “ReplaceSpaces” (or whatever you want; the macro’s name cannot contain spaces). Hit OK.

This is the part where you would do whatever you want the macro to do. But in this case, now choose Tools -> Macro -> Stop Recording. You’ve recording nothing.

Now choose Tools -> Macro -> Macros. In the dialog box, click your newly created macro’s name and then click Edit.

Now replace this empty macro —

End Sub

Sub ReplaceSpaces()

‘ ReplaceSpaces Macro

End Sub

— with this, which you can copy right from this webpage:

Sub ReplaceSpaces()

With Selection.Find

.ClearFormatting

.Replacement.ClearFormatting

.Text = ” “

.Replacement.Text = ” “

.Forward = True

.Wrap = wdFindContinue

.Format = False

.MatchCase = False

.MatchWholeWord = False

.MatchWildcards = False

.MatchSoundsLike = False

.MatchAllWordForms = False

.Execute Replace:=wdReplaceAll

End With

End Sub

You can take this opportunity, by the way, to change this macro to any search/replace you want; it doesn’t have to replace two spaces with one. For example, to do a search for “fish” and replace every occurrence with “chips,” you’d change the lines shown here as shown:

.Text = “fish

.Replacement.Text = “chips

Close the Visual Basic Editor.

Next time you want to run this search/replace macro, choose Tools -> Macro -> Macros, and double-click the name of the one you want to run.

Or, to make things easier, you can assign your macro to a keyboard shortcut or turn it into a toolbar button.

Once again, automation takes over our jobs!

David Pogue, tech columnist for Yahoo Finance, welcomes nontoxic comments in the comments section below. On the web, he’s davidpogue.com. On Twitter, he’s @pogue. On email, he’s poguester@yahoo.com. You can read all his articles here, or you can sign up to get his columns by email.

Advertisement