Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Understanding Functions.

Understanding Functions

Written by Allen Wyatt (last updated May 1, 2021)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


1

You already know that you can use subroutines in your macros. VBA also allows you to define functions that can be used in your macros. The difference between functions and subroutines is that functions can return values, whereas subroutines cannot. Consider the following VBA macros:

Sub Macro1()
    TooMany = TestFunc
    If TooMany Then StatusBar = "Too many pages"
End Sub
Function TestFunc()
    TestFunc = False
    If Selection.Inforamtion(wdNumberOfPagesInDocument) > 10 Then
       TestFunc = True
    End If
End Function

There are two macros in this code. One is the function (TestFunc) and the other is a macro (Macro1) that is used to invoke the function. When the function is executed, it can do anything that is done in a regular macro. In this case, it performs a test that results in the TestFunc variable being set to either True or False. Remember that this variable name is the same as the function name. This is the value that is returned by the function to whatever program called it.

Which brings us to the Macro1 macro. Remember that the TestFunc function can appear on the right side of the equal sign. This makes functions very powerful and an important part of any program. VBA executes the function and returns whatever value is appropriate from that function and assigns it to the variable on the left side of the equal sign (TooMany). The program then acts upon the value returned.

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 (12961) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Understanding Functions.

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

Sharing Your Workbook

Need to allow others to contribute to your Excel workbook? It's easy to do if you just share it. This tip provides an ...

Discover More

Losing Formatting

When you save a workbook, you expect Excel to remember the formatting you applied in the worksheets in that workbook. If ...

Discover More

Removing Line Numbering

If you've added line numbers to your document, at some point you may want them turned off. Here's how to disable line ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Clean Up Your Macro List

Macros are part of the document and template that are loaded into memory when you open a file. If you have "junk" macros ...

Discover More

Changing What Is Pasted in a Dialog Box

When you record a macro, Word very literally records what you do. This includes filling in various settings in dialog ...

Discover More

Changing Built-in Word Commands

Want to replace Word's internal commands with your own macros? It's easy to do if you know the key discussed in this tip.

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 one less than 9?

2021-05-01 08:54:22

Simao Campos

Please note the typo: Selection.Inforamtion -> Selection.Information


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu 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.