If Then Else Workout
- 07:13
Workout to demonstrate If Then Else in Excel VBA.
Transcript
In this workout, we must use four each looping procedure to write VBA code to fill each cell in the range, range one according to the following rules.
If the value of the cell is less than zero, we fill red.
If the value of the cell is less than 10, we fill yellow.
And if the value of the cell is greater than 10, we fill green.
Let's write our VBA code.
We're going to write our VBA code into a module.
So first we insert our module coding for our modules off with sub, and we will write a name for our sub procedure.
Let's call this one workout 3.
It's a really good idea to plan out our code before we begin writing in the VBA editor.
We need to build for each next looping procedure to evaluate the contents of each cell in a range.
This time the instructions within the four each next loop will include if, then, else, code.
Two variables are required and they are two range variables.
The first range variable will represent each cell, let's call it CL in the range.
The second range variable will represent the Excel range, range one.
Let's declare those variables.
We declare our CL each individual cell within the range object variable as a range object, and we declare a variable that we'll name quarters one as another range object quarters.
One is our range object variable that we want to take the range equivalent to range one in the front end of Excel.
Now, in order to do that, we have to use the syntax set.
We set our range object variable quarters, one to be equal to the range, range one in the front end of Excel.
Next, we'll write our looping procedure, and we need for each looping procedure to move through each CL in the range object for each CL in our range object variable quarters.
One within our looping procedure.
We are going to use if then else coding to assess the value of each of those CL range objects within our quarter one range.
The first thing that we do is state our condition. If the value property of that cell is less than zero, then what do we want to do? These are the instructions that we want carried out.
We want the cell range object.
Its interior property and the colour property of that interior to be assigned VB red.
We want to colour that interior in red.
If the condition above isn't true, we move on to test the next condition.
Else if CL value is less than 10.
Then what instruction do we want carried out? Will we want the interior property and the colour property of that interior to be yellow? The range object CL, its interior property and the colour property of that interior is equal to VB yellow.
Finally, if the second condition isn't true, the following structure should be carried out.
This final instruction we start with else, and the instruction should be the interior property, the colour property of the interior property of that range object is assigned to be VB green.
Let's complete our if then statement with the words and if we have to close our loop so that we can go back to our four each next procedure to evaluate the next CL in the range.
And we do that by typing in next CL.
Let's go back to the front end of Excel and execute this procedure.
We go to the developer tab, we view our macros and there is the procedure that we've just written out. Three, we select run, and you can see that each of those cells in the range has been evaluated and it has been assigned to red interior.
If the value is less than zero, the interior is shaded with yellow.
If the value is less than 10 in all of the conditions, the value is greater than 10.
The interior is shaded to be green.