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 macro:
Sub Macro1() TooMany = TestFunc If TooMany Then MsgBox "Too many columns selected" End Sub
Function TestFunc() TestFunc = False If Selection.Columns.Count > 10 Then TestFunc = True End If End Function
The macro (Macro1) calls the TestFunc function. This function returns either the value False or True, depending on a test it performs. Macro1 then acts upon the value returned. Notice that the function name can appear on the right side of an equal sign. This makes functions very powerful and an important part of any program. Within the function the result is assigned to TestFunc, which is the name of the function itself; this is the value returned by the function.
As with subroutines, you can also pass parameters to your functions. This is illustrated in the following macro:
Sub Macro1() A = 12.3456 MsgBox A & vbCrLf & RoundIt(A) End Sub
Function RoundIt(X) As Integer RoundIt = Int(X + 0.5) End Function
This simple macro (Macro1) defines a number, and then uses a message box to display it and the result of passing the number to the RoundIt 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. 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). 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.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11765) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365. You can find a version of this tip for the older menu interface of Excel here: Understanding Functions in Macros.
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!
Need to normalize your data in some way so that all your values are in a given format? This tip presents a number of ...
Discover MoreGot a workbook that has lots and lots of macros associated with it? Here's a way you can get a list of all of those ...
Discover MoreIf you have a series of values in a column, you might have a need to separate the values into even values and odd values. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-04-24 09:51:49
Kevin Moynihan
I often write code using the 4 or 5 line approach as opposed to the one line.
And the reason is that often the code is inside a repetitive loop, where the operation will be evaluated dozens or hundreds
of times. So good programming says I should reset the variables each time I go through to evaluate the expression.
However, if the expression is inside a function, and the function is in a loop, then you could write the function suing the simplest form possible
2020-02-08 15:55:11
John Mann
R Grealish. I'm very much a novice at VBA (If I've even reached novice level). In your last example, I'm inclined to wonder what would be the value of "reult" if the logical test fails, and result has not been set to false before hand?
2015-12-08 10:21:47
R Grealish
I think the point of my comment has been misunderstood (My fault in not expressing my self well).
I was not commenting on the specific tip whose purpose I fully understand. I took the code in the function as an example of a more general point I was trying to make which was why use more code than you need to. I will construct an example without using the code in the tip.
Consider the following VBA code fragment (which is not necessarily part of any function or subroutine)
result = false
if a > b then
result = true
end-if
In my view this code fragment could be written more succinctly as
result = a > b
2015-12-07 13:00:27
R Lowe
R Grealish, in fact a function call isn't necessary. The function call could have been replaced with:
If Selection.Columns.Count > 10
Then MsgBox "Too many columns selected"
End If
The point of this tip is just to demonstrate the difference between subroutines and functions.
2015-12-07 12:55:53
CJ
The example for TextFunc should work, but best programming practices suggest that the TextFunc function be defined as returning a Boolean data type.
2015-12-05 07:19:59
R Grealish
In many code examples, code of the following structure appears
TestFunc = False
If Selection.Columns.Count > 10 Then
TestFunc = True
End If
(taken from the tip).
It is unclear to me why the simpler and equivalent code is not used
TestFunc = Selection.Columns.Count > 10
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments