|

How to Write a Visual Basic Function Procedure

This is from Microsoft Excel 2000 Visual Basic Help, with changes and additions.

A Function procedure is a series of Visual Basic statements enclosed by the Function and End Function statements. A Function procedure is like a Sub procedure, but a function can also return a value. A Function procedure can take arguments, such as constants, variables, or expressions.  These can be passed to it by a calling procedure or the spreadsheet.  If a Function has no arguments, its Function statement must have empty parentheses. A function returns a value by assigning a value to its name in one or more statements of the procedure.

In the following example, the Celsius function calculates degrees Celsius from degrees Fahrenheit. When the Main procedure calls the function, it passes a variable containing the argument value to the function. The function returns the result of the calculation to the calling procedure, which displays it in a message box.

Option Explicit

Sub Main()
  Dim Temp&
  Temp = Application.InputBox(Prompt:= _
    "Please enter the temperature in degrees F.", Type:=1)  ' Type:=1 = Number
  MsgBox "The temperature is " & Celsius(Temp) & " degrees C."
End Sub

Function Celsius&(fDegrees As Long)
  Celsius = (fDegrees - 32) * 5 / 9
End Function

Note: Using Option Explicit, as above, is highly recommended to reduce errors in your code.  But then, the code given by Microsoft for Sub Main above will neither compile nor run.  You need the extra line above, Dim Temp&, to define the long integer variable Temp&.

Above, we see a Function called from Visual Basic code.  You can also call it from the Excel spreadsheet, just as you would with a built-in function.  For example:

Note that the type declaration character (“&” for Long Integer) is not used here.  Thus, we use =Celsius(A2) and not =Celsius&(A2).

Similar Posts

  • |

    Visual Basic Naming Rules and Tips

    This is an excerpt from Microsoft Excel Visual Basic Help, with tips and corrections. Use these rules when you name procedures, constants, variables, and arguments in Visual Basic: 1.        Capitalization and Case-Sensitivity Visual Basic isn’t case-sensitive, but it preserves the capitalization in the statement where the name is declared. If you declare variables with the…

  • Linear Interpolation in Excel

    We often need to find a value between or near two other known values. If we do not have a more accurate curve, a straight line will have to do. This is Linear Interpolation (or extrapolation). Excel is a good way to do it. For example, I read our electricity meters weekly. This lets me…

  • |

    How to Pass Arguments Efficiently in Visual Basic

    This is from Microsoft Excel 2000 Visual Basic Help, with changes and additions. All arguments are passed to procedures by reference, unless you specify otherwise. This is efficient because all arguments passed by reference take the same amount of time to pass and the same amount of space (4 bytes) within a procedure regardless of…