When you have written a User Defined Function (UDF) in VBA, the function can be found in the function wizard under the "Custom" category. You can assign a category to the function by changing the "macro properties" of the UDF, however, you will not be able to specify a description for any of the arguments.
So, on to creating our first function, if you aren’t already in the VBE (Visual Basic Editor) then hold down Alt and press each of these keys in turn F11 (gets you to the VBE), I (selects the insert menu), M (selects module). Now you have successfully opened the VBE and inserted a Standard Module we can begin to build a very simple UDF.
Firstly at the top of our new module we should enter “Option Explicit” (without the quotes), this forces us to declare our variables, it will prevent us from incorrectly typing the name of an existing variable, and help keep our code tidy, using Option Explicit is part of a coding convention adopted by professionals – if it’s good enough for them then it’s good enough for us!
Creating a FUNCTION is much like creating a SUB and we start the same using function instead of sub:
N.B Because we will be working with numbers I have set the Data type to Double, that’s my preference as it can house larger numbers, you could use single, integer or long, although you may as well skip integer as execl converts integers to long for calculations anyway!
royUK suggested code change for exchange rate in cell rather than code
Option Explicit
Function Conv(gbp As Double, rte As Double) As Double
'create our calculation
Conv = gbp * rte
End Function
Now that we have created our currency conversion function we can put it to good use!, press Alt+Q to get back to our worksheet, in B1 enter our new function, it’s done like this: =Conv(A1,1.6) (A1 will be the cell value we are applying our function to), in A1 enter a figure and hit enter, you should now have the value of A1 multiplied by 1.6.
One thing to remember about Functions is that they can only return a value, either to a cell or back to a macro for further use.