Replacing Text in a Macro

Written by Allen Wyatt (last updated April 3, 2020)
This tip applies to Word 97, 2000, 2002, and 2003


If you are writing macros, you probably do a lot of work with string variables. A common operation related to string variables is the need to replace one occurrence of characters in a string with another occurrence. For instance, consider the following string:

"This is my string of characters."

You might want to replace "of" with another word, such as "that has". The following general-purpose function can do just that, and much more:

Function RepText(sIn As String, sFind As String, sRep As String) As String
    Dim x As Integer

    x = InStr(sIn, sFind)
    While x > 0
        sIn = Left(sIn, x - 1) & sRep & Mid(sIn, x + Len(sFind))
        x = InStr(sIn, sFind)
    Wend
    RepText = sIn
End Function

As an example of how to use the function, you can do the following:

sTemp = "This is my string of characters."
sTemp = RepText(sTemp, "of", "that has")

When this code is executed, sTemp will contain the string "This is my string that has characters."

In my routines, I often utilize a function just like RepText to remove extra spaces (replacing a two-space string with a single-space string) or replacing multiple spaces with a tab character. How you use it, of course, is entirely up to you.

Those who have used only the latest versions of VBA may be curious as to why I would use a function such as RepText rather than the built-in Replace function. While the Replace function does essentially the same thing as RepText, its addition to the VBA arsenal is a relatively new occurrence. If your macros have even the slightest possibility of being used in older versions of Word, then using Replace runs the risk of crashing your macro—it won't be available in those versions.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (811) applies to Microsoft Word 97, 2000, 2002, and 2003.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Task Pane Doesn't Appear Properly

Excel 2002 and 2003 include a Task pane that provides quick access to common tasks. The Task pane is normally visible ...

Discover More

Capitalizing Just a Surname

Changing the capitalization of text is, believe it or not, a common task in Excel. Common or not, it can be frustrating ...

Discover More

Negatives in Pie Charts

Pie charts are a great way to graphically display some types of data. Displaying negative values is not so great in pie ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (menu)

Changing the Default Drive

Macros can be used to read and write all sorts of files. If those files are on a different drive than the current one, ...

Discover More

Controlling the Italic Text Attribute

If you are formatting your document by using a macro, you may need to make some of your text italics. You do that by ...

Discover More

Determining the Current Page Number

While your macro is processing the text in your document, you may need a way to determine the current page number where ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 1 + 1?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.