Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, and 2013. 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: Passing Parameters to Functions.

Passing Parameters to Functions

by Allen Wyatt
(last updated September 7, 2016)

4

When you create your own functions, it is often helpful to pass parameters to the function. These parameters can be used either as data that you want the function to act upon or as settings used to control how the function does its work. How you pass parameters to functions is illustrated in the following macros:

Sub Macro1()
    A = 12.3456
    Status.Bar = A & "     " & Round(A)
End Sub
Function Round(X)
    Round = Int(X + 0.5)
End Function

This simple macro (Macro1) defines a number and assigns it to the variable A. It then prints that number and the result of passing the number to the Round function; the output is 12.3456 and 12. Notice that the parameter should be passed to the function within parentheses.

Also notice that the function does not use the same variable name as it was passed. (The variable A is passed to the function when it is invoked; within the function this value is referred to by the variable name X.) This is because VBA reassigns the value of X (what the function needs) so it matches the value of A (what the program is passing to the function).

VBA allows you to specify multiple parameters to be passed to a function. The parameters simply need to be separated by commas in both the declaration of the function and whenever the function is called. The important thing to remember in passing parameters to functions is that your program must pass the same number of parameters as the function expects, and the parameters must be of matching types and in the proper order.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12962) applies to Microsoft Word 2007, 2010, and 2013. You can find a version of this tip for the older menu interface of Word here: Passing Parameters to 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

Selecting a Language

Need to format a paragraph (or some selected text) so that it is a language other than English? You can do so easily by ...

Discover More

Searching Comments

Need to find that misplaced comment in your worksheet? It's easy to do using the Find and Replace capabilities of Excel.

Discover More

Altering the Displayed Format of Numbers to the Nearest 100

Want information in a worksheet to be formatted and displayed as rounded to a power of ten? You may be out of luck, unless ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Moving the Insertion Point to the End of a Line

When writing a macro to process the text in a document, you may need to move the insertion point to the end of a line. This ...

Discover More

Moving to the Start or End of the Real Document

The main body of your text is only one part of what makes up the entire document. Documents can consist of other elements, as ...

Discover More

Toggling Font Assignments in a Macro

If you need to quickly switch a text selection from one typeface to another, one way you can do it is with a macro. 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}] 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 8 - 2?

2016-09-13 07:18:59

Mandy

I understand the principle.
But about this specific instance it does not work
How can you really change the text in the status bar in Word 2013?
Thanks
Mandy


2016-09-07 16:02:20

Peter

Thanks to both Allen and Bryan. Bryan, I get why you've added your tips; you want us to do it right. And you have really good advice there. But Allen's initial tip was really helpful to me as a beginner. My issue is not about doing it right, it's about doing it at all. And the tip was simple enough that I can see a concept that I haven't really understood from other articles, see how I might actually get started. Between the two of you, well done!


2014-02-18 09:41:26

Bryan

Bah, sorry about the Wall O' Text. I wrote it in Word in case my browser crashed, and forgot to add back in the extra line breaks.


2014-02-18 09:39:38

Bryan

Couple of things here:

1) You can, and really, really should, dimension all of your variables. Instead of just "A = 12.3456", you should first have a line "Dim A as Double". (Option Explicit will avoid this error). You should also dimension variables within the function call: "Round(X As Double)". If you don’t, then you will be able to pass non-numbers into your Round function, thus causing an error. If X is dimensioned it’s a lot more likely that you will catch the error upon compiling instead of somewhere down the line. While you’re at it, you can also dimension the output: "Round(X As Double) As Double".
2) You can name the parameter when calling the function if you choose: "Status.Bar = A & " " & Round(X:=A)". Note the semicolon before the equals sign. If you use this technique, you no longer have to call the variables in the exact order. For instance, if you had a function defined as "Round(X as Double, Y as Double, Z as Double)" [terrible variable names, btw], you could call the function as "Round(Z:=1, Y:=2, X:=3)" and it would work exactly like you expect it to.
3) You also aren’t limited to only required variables. The Optional keyword allows you to make a parameter or parameters optional. These have to be last in the order of variables (in other words, you can’t have an optional parameter followed by a required parameter). Simply put, a function won’t throw up an error if you don’t put in the optional parameter. You can even give the parameter a default value. For instance, "Round(X as Double, Optional Y as Double, Optional Z as Double = -1)". If you called "Round(12.3456)", the function would still work. Y would default to 0 (the default for uninitialized Doubles), and Z would default to -1. You can also skip variables by leaving a comma, or by calling them out directly: "Round(12, , 34)", "Round(12, Z:=34)".
4) Lastly, you are not required to have a set number of inputs. If you don’t have any optional parameters, you can use the ParamArray keyword to make the last parameter a variant array that accepts any number of arguments. So You could declare a function "AddEmUp(ParamArray Nums())". Then you can call the function in the following ways: "AddEmUp()", "AddEmUp(1)", or "AddEmEup(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)".
5) The fifth thing (I guess I lied about the last one being the last one) is that if your function isn’t being used to return a value, you don’t need to use an equals sign, and you don’t need to use parenthesis. Reasons for having a function like this are nuanced; however, subroutines also accept parameters, and they never return a value, so you can choose to call your subroutines in this manner. Say you had a subroutine declared as "DeleteTheCells(RangeOfCells as Range)". You could call it as such: "DeleteTheCells Range("A1:A10")".


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.

Newest Tips
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.