In today’s article we will show you how to calculate the total cost for a given weight using the Excel VLOOKUP function. This function will help us to find the appropriate unit price for that weight and then we can use the simple multiplication “unit price * weight” to get the total cost.
Suppose we want to purchase a batch of something, unit prices are different depending on the weight bands, usually the unit price becomes cheaper as the weight increases.
If the correspondence between weight band and unit price is provided in a table in excel worksheet, how can we find the matching unit price and calculate the total price if we know the weight? Can we just create a simple formula to get the total cost?
Look at the example below. To calculate the total cost of something with a weight of 120kg.
In this example, the formula is:
VLOOKUP function is a vertical lookup function in Excel, it runs a column-by-column lookup that eventually returns the value corresponding to the looked-up value in the specified column.
- lookup_value is the value that needs to be looked up. Make sure it is in the first column of the table. lookup_value can be a numeric value, a reference, or a text string.
- table_array is the table in which the data needs to be looked up. Use a range reference to or a table name to represents the table.
- col_index_num is the column number of the data to be looked up in the table_array.
- range_lookup provides two modes for VLOOKUP; If range_lookup is TRUE or 1, the VLOOKUP function will look for an approximate match; if range_lookup is FALSE or 0, VLOOKUP returns an exact match; and it returns an error value #N/A if not found. If this argument is omitted, the default value is 1, which means that VLOOKUP returns an approximate match.
As we mentioned above, the total cost is equal to the unit price * weight. In this example, the unit price is not a fixed value, it is determined by the weight, and since the weight is given by D2, we need to know the unit price of D2, and we use Excel VLOOKUP function to retrieve the corresponding unit price.
So, in this formula:
VLOOKUP(D2,A2:B8,2) – unit price
D2 – weight of the input
- Lookup_value – D2 (120kg)
- Table_array – A2:B8
- col_index_num – 2 (returns the unit price as unit price in the second column)
- range_lookup – 1- Approximate match (If this argument is omitted, the default value is 1)
Step#1 VLOOKUP finds the row of entered weight “120”
In this example, “120” is not listed in the first column of the table. Since the value of range_lookup is 1, VLOOKUP returns an approximate match. In approximate match mode, Excel VLOOKUP function will search for “120” in the first column until it encounters the first value greater than it, and then return to the previous row.
In this example, in “Weight” column, Excel VLOOKUP function compares each weight against with the lookup value “120”, and when coming to cell A6, it encounters the first value greater than “120” which is 150, so it returns to A5.
(you can compare the two pictures for better understanding the approximate match)
Step#2 VLOOKUP returns unit price in the row of weight “120”
In the previous step, we know that the row index of weight “120” is 5. Since the column index is 2, Excel VLOOKUP returns the intersection in row 5 and column 2 of table A2:B8. The intersection is B2, value in B2 is 1.23. So VLOOKUP function returns 1.23 finally.
Step#3 Calculate the formula =VLOOKUP(D2,A2:B8,2)*D2
The formula is equivalent to
In step#2, we know VLOOKUP returns 1.23, so the total cost in this example is 1.23*120=147.6.