User Defined Functions Part 1 Workout
- 03:53
A workout to demonstrate building a user-defined function.
Transcript
In this workout, we are required to create a Fahrenheit calculation function that has one parameter as its argument, the temperature in Celsius.
And we're helpfully given the formula to convert a temperature in degrees Celsius into a temperature in degrees Fahrenheit.
Let's go into the Visual Basic Editor and create our function.
We're going to write our function in a module, so we insert module.
Now, rather than starting our function off with the word sub, we start our function off with the word Function.
Let's name our function, DegF.
The function will have one argument, the parameter of degrees Celsius.
We want that parameter of degrees Celsius to take as many possible numbers as we can think of, so we'll declare that as a double.
Double data types can handle large numbers as well as decimals to provide us with the biggest range of flexibility, the output of the function will also be a double.
Again, we wanted to hold the widest range of numerical values, including integers and decimals.
We can then write our function.
The cell selected will pass values to the function here.
Our function will be DegF.
Our function name will be equal to 32 plus the cell selected ie, the cell that we have referred to as degrees Celsius, multiplied by 1.8.
Let's write that in 32, plus our one function parameter of degrees Celsius, multiplied by 1.8.
Once we've pressed enter, we can go back into the front end of Excel and test our function works back in the front end of Excel.
I'm going to go into cell C 9 and type equals. I'll begin typing the name of our new function, DegF.
You'll see our function appear.
Our function has just one required argument.
This will be the value of the degrees Celsius parameter, and that value is passed into our function argument where 32 plus the value of the degrees Celsius parameter here, 36.9 is multiplied by 1.8.
When we press enter, we can see that the functions worked, and 36.9 degrees Celsius is equal to 98.4 degrees Fahrenheit.