Excel VBA Functions Workout
- 04:49
Workout demonstrating how to write Excel VBA code for an Excel function.
Transcript
In this workout, we are required to write VBA code to use the Excel function V lookup to display the revenue under the scenario indicated.
Let's take a look at the information we have.
We have a table of scenarios, low, mid, and high, and then their revenues.
The low scenario has revenue of 10,000, the mid 11,000 and the high of 12,000.
In cell C 13, we are given the selected scenario, and this is the high scenario in cell C 15.
We need to return the matching revenue for that high scenario.
Let's go into the Visual basic editor and write some code developer, visual basic, insert module.
We begin our procedure with the word sub And we'll call our procedure workout 4.
As soon as we press enter Excel, VBA automatically adds the words end sub.
We want to use Excel's worksheet function to return the appropriate value.
First of all, we need to identify the range object where we want that value to be placed.
We want that value to be returned into cell C 15 range C 15, and we can see that auto list members is super, super useful here.
Range C 15 is the range object and we want to apply a value to the value property.
When we dial in dot value, we can see that the value property is relevant to that object and we want to assign it with where we get this V lookup answer from.
In order to use the Excel worksheet function of V lookup, we should start this off with application.worksheet function.
Which worksheet function do we need? We need the V lookup function and we can see the V lookup just here.
V lookup has a number of arguments, and those arguments are exactly the same as the front end Excel.
Which value are we looking up? Well, we're looking up the value in range C 13.
Let's specify range C 13.
Next, after specifying the value property of range C 13, our next argument is where do we want to look for that value in C 13? Well, we want to look for it in that list of all of the scenarios and the second column that contains their values and that goes from range B 7 all the way out to C 10.
Again, similar to the front end of Excel.
We then need to tell the function once we've matched the value in C 13 into that table, which column do we want to return? We want to return the second column.
The final argument is a zero because we want an exact match pressing enter.
We've completed our code.
Let's go back to the front end of Excel and test this out.
Developer visual basic.
We can either press the run button, go back into the front end.
It is exactly the same as going into developer and macros and choosing to run workout number fours procedure, and of course, we've found the word high within this range.
We've locked up that matching high to its relevant number in the second column of the table to give us the answer of 12,000.