User-defined Functions using Visual Basic Applications in Excel - Programming Syntax
(Page 3 of 4 )
As with any piece of programming code, user-defined functions in MS Excel also follow a set of rules when it comes to proper syntax.
Below is the standard syntax:
Function Functionname(Variable)
<Visual basic source code of your function here>
End Function
It should start with the syntax “Function” and then end with the “End Function.”
This tells Visual Basic that we are running an Excel function. Concepts will be very hard to understand without real-world examples.
We would like to make a user-defined Excel function that will automatically compute the monthly electricity bill (in dollars) given the power rating of the home appliance, the number of appliances involved (units) and the number of hours in operation.
In classic Microsoft Excel using built-in functions, the following is the Excel table using the lengthy computational processes:

Note that there are a lot of computations involved, and it takes up a lot of computational space. We will attempt to write a user-defined function in Visual Basic that will compute the cost automatically given the power rating (watts) of each home appliances.
Next: Basic Code Example >>
More Visual Basic.NET Articles
More By Codex-M