An Example of a Function
A simple function which is used by user is to convert Feet into Metres. To turn Feet into metres then you would use this calculation – Metres = Feet * 0.3048
Below you will see how you would write this in VBA, each part has its own role to play and is described to you in more detail below.
VBA keyword – Function/ End Function – these are the VBA keywords with this bit of code and it identifies the procedure as a user-defined function. The End Function part shows us the end of the function.
Function Name – FeetToMetres – this is the name it is giving and how it is referred to in other procedures.
Argument and Argument type – (intFeet) – this is the data that will be passed to the function. There is the possibility to have more than one argument and they would be separated by commas or none depending on the circumstances. In the example above the argument will receive the value in inches. The type of Single indicates the type of data to be received.
Comment – this is a line to describe what the function will be used for and will not affect the calculation.
Calculation – This code must use the value of the argument in a calculation before assigning the resulting value to the function. The name of the function matches the name that the result of the calculation is assigned to.
You could attend a Excel VBA course which will teach this and more in a lot more detail.