877 User-Defi ned Functions with VBA
Notice the following:
A. The word “[break]” appears in the title bar.
B. The offending symbol is selected.
C. The function line is highlighted and pointed to by an arrow in the
margin.
Because VBA found an error while trying to execute the function, it
moved into a special execution mode called debug-break mode. For now
all we need to do is get out of this special mode so we can get on with
our work. We do so by clicking the appropriate icon on the VBA toolbar
(a small dark square). Now you can fi x the function and use it.
36.5 Conditional Execution: Using If Statements in VBA Functions
In this section we explore the If statements available to you in VBA. Not
all things in life are linear, and sometimes decisions have to be made. If
statements are one way of making decisions in VBA.
36.5.1 The One-Line If Statement
The one-line If statement is the simplest way to control the execution of
a VBA function: One statement is executed if a condition is true, and
another is executed if a condition is not true. The complete condition
and its statement should be on one line. Here’s an example:
Function SimpleIf(Parameter)
If Parameter > 5 Then SimpleIf = 1 Else SimpleIf
= 15
End Function
We can now use the function SimpleIf in Excel. When Parameter is
greater than 5, SimpleIf returns 1, and when Parameter is less than or
equal to 5, SimpleIf returns 15: