Declaring Variables Workout
- 07:18
Workout to demonstrate how to declare variables in VBA code and use variables in a formula.
Transcript
In this workout, we are required to write VBA code to convert an input temperature in degrees Celsius into a temperature in degrees Fahrenheit.
We should declare a local variable named Celsius input and assign that variable a value of 20.
We are also rather required to declare a global variable named Fahrenheit Calc.
We should then display our output temperature in degrees Fahrenheit in cell B 9.
Let's go into the Visual Basic editor via the developer tab pressing visual basic.
We will write our code into a module, so we go insert module.
Now you'll notice that the words option explicit automatically appear at the start of my module.
This means that in any coding that I write, we are required to declare variables.
We can either write option explicit freehand at the top of our module, or if you go to tools, options, and check the box That's called require variable declaration.
Checking this box will automatically add the wording option explicit to the start of all of your modules, and this will mean that you have to state the variables in your code.
The first variable that we are required to declare is a public variable, and we do that by writing the word public.
Public Fahrenheit Calc, Fahrenheit Calc is the name of our variable that will take the value of the temperature in degrees Fahrenheit, and we will declare this as a double, as this variable has been declared as a public variable, it can be used and recognized by all of the sub-procedures within the module and all of the modules within our workbook.
Let's now write our sub procedure to carry out the required instructions.
As always, we begin our sub procedure with the syntax sub and we'll call our sub procedure work out one.
The first thing that we should note is a comment so that we can remind ourselves and other users of the file what the formula is to work from degrees Celsius to degrees Fahrenheit.
The formula for going from degrees Fahrenheit or rather degrees Celsius to degrees Fahrenheit is apostrophe for the comment F degrees Fahrenheit Equals 32 plus 1.8 times by the temperature in degrees Celsius.
As we've put an apostrophe in front of the code, the code turns green.
That line of code won't be executed, but it sits there as a comment for us and other users of our spreadsheet to note the formula.
The next thing that we are required to do is declare our local variable.
We use that by writing dim and the name of our local variable is Celsius input.
I'll declare this variable to take a data type of double.
It can take any number, integer or decimal that will give us the most flexibility.
The next thing that we have to do is assign our variable of Celsius inputs a value, and we're going to assign Celsius inputs a value of 20.
Next, what we need to do is we need to state our calculation of the variable Fahrenheit calc.
Fahrenheit Calc is equal to 32 plus 1.8 multiplied by our variable Celsius input.
We are assigning the value of 32 plus 1.8 times by 20 the value of Celsius input to our variable Fahrenheit calc.
Now you'll notice that I input the word Celsius input without the capitalized I.
When I press enter excel VBA recognizes the name of my variable and capitalizes it at appropriate.
This is a great way to make sure that we're spelling our variables correctly and by using a name that's sensible, we can also keep track more methodically of what those variables mean.
Next, once we've assigned a value to our variable Fahrenheit calc, we then want to put that value of the variable into cell B 9 in our spreadsheet.
We do that by stating the range object cell B 9 and its value property, and we want to assign that value property with the value of our variable Fahrenheit calc.
I input Fahrenheit calc in lowercase.
Hopefully excel VBA will capitalize this as it recognizes the name of the variable.
Our sub procedure is complete.
Let's go back into excel VBA and execute this sub procedure to see if it works.
Back in the front end of Excel, we go to developer view macros and we can see our macro workout one.
Let's run this macro, and in cell B 9 we have the 68 degrees Fahrenheit.
That is our required output.
20 degrees Celsius is equivalent to 68 degrees Fahrenheit.