Functions like procedures are used to perform a task. The difference between the two is that functions return a value which is usually the result of some type of computation while Procedures do not. For example, when you use Excel's built-in SUM function you provide the cells that you want summed for the arguments then Excel returns the result of that computation.
Functions can't change anything on a worksheet. Their sole purpose is to determine the value to be returned and then return that value. You can't use a function to change the value in another cell or apply any type of formatting to any cells. Excel will return an error if you attempt to do so.
The Syntax for a Function is:
[AccessModifier] Function FunctionName([parameters]) As DataType
[Code]
End Function
Syntax Description
AccessModifier - can be Public, Private, or Static
DataType – This is the type of the value being returned such as Integer, String, etc.
Public -The function can be accessed from any other procedure in any module in the current workbook. If you don't specify an Access Modifier: Public will be used by default.
Private -The function can only be accessed from other procedures that are in the same module. If you specify the access modifier as Private your function will not appear in the AutoComplete drop down list nor will will it be available from the Insert Function dialog box.
Static -The values in variables are normally cleared when the procedure has finished running but if you specify Static for the access modifier the variables in the procedure will retain their values.
Specifying the DataTypeThe function starts with the Function statement and ends with the End Function. Because a Function returns a value, you need to specify the type of that returning value.
Rules for Names Sub Procedures and Functions The rules for assigning names to Procedures and Functions is the same as those for assigning names to variables.*Note - Even though Excel allows you to name a User Defined Function with the same name as a built-in function, don't do it. Excel's function will override yours and could cause problems.
Public Function CUBENUMBER(value) As Long
'Returns the cubed value of a number
CUBENUMBER = value * value * value
End Function
Category Number | Function Category |
0 | All |
1 | Financial |
2 | Date & Time |
3 | Math & Trig |
4 | Statistical |
5 | Lookup & Reference |
6 | Database |
7 | Text |
8 | Logical |
9 | Information |
10 | Commands (normally doesn't appear in Insert Function) |
11 | Customizing (normally doesn't appear in Insert Function) |
12 | Macro Control (normally doesn't appear in Insert Function) |
13 | DDE/External (normally doesn't appear in Insert Function) |
14 | User Defined |
15 | Engineering (normally doesn't appear in Insert Function) |
16 | Cube |
17 | Compatibility |
Argument used in MacroOptions | Description |
---|---|
Macro | Enter the name of the Function |
Description | This is the description to be displayed with the Function |
Category | This can be a number associated with one of the Excel categories or it can be a category name that you create. |
ArgumentDescriptions | Descriptions for each of the arguments used in the Function you created. This is an array. |
The ArgDesc provides a description for the argument. Since there is only one argument for CUBENUMBER it will be ArgDesc(0).
Public Sub Functioninfo()
Dim ArgDesc(0) As String
ArgDesc(0) = "a number that you want to find the cube of"
Application.MacroOptions _
Macro:="CUBENUMBER", _
Description:="Function takes a value and cubes it.", _
Category:="My Category", _
ArgumentDescriptions:=ArgDesc
MsgBox("Function information has been added")
End Sub
The add-in module no longer has a visible workbook associated with it, but you can use the code from the modules in the current workbook.
If you are going to share your workbook with others you will have to provide them with the Add-In as well.