Modules in Excel Part2
Modules in Excel Part2
The VLOOKUP Function (which stands for "vertical lookup") is one of the most useful functions
in Excel, and is a fast way to look up a value when your information is arranged in a table with
rows and columns. The VLOOKUP function has four arguments (values in the function separated
by commas) and because of this people often think VLOOKUP is complex. Once you try a few
practice problems with VLOOKUP you'll see how simple it really is.
If your data is arranged in a table with rows and columns, the VLOOKUP function will tell you a
value in one row if you know a different, corresponding value to the left in that same row (the
VLOOKUP function only looks from left to right). For the VLOOKUP example below, say you have
a table with columns for employee ID, hire date, and employee name in your Excel spreadsheet,
as shown in the image below. Each row represents a different employee. Say you want to retrieve
the name of the employee with ID 1234 using VLOOKUP.
1. Lookup_value: This is the value that you know, which you will use to locate the value that
you do not know. In this example it is the employee ID, which is 1234
2. Lookup_range: This is the entire table, also called the lookup table or table array, that
we are searching. We could say A1:C6. If this was a long list we could use the entire
columns A:C
3. Column_number: The columns in your lookup_range table are numbered from left to
right, starting with 1. The VLOOKUP function will locate the lookup_value in column 1 (far
left) and return the value in the same row but in the column specified by column_number.
The employee ID column is 1, the hire date column is 2, and the employee name column
is 3. Note that VLOOKUP only searches from left (starting in column 1) to right. Because
we want VLOOKUP to return the employee's name, we will specify 3 for column_number.
4. Approximate_match: This is the only optional argument, though it's recommended that
you specify it as false (it defaults to true) when you know the lookup_value. In our case
we can say false or 0; we want an exact match on 1234. The default value is true, so if
you leave this blank Excel will tell VLOOKUP will look for approximate matches, which we
typically don't want except for special cases.
So, in order to use the VLOOKUP function in Excel to find the name of the employee whose ID is
1234, you could use the formula =VLOOKUP(1234, A1:C6, 3, false). See image below for details.
Note that the value you know (the first argument) must always be in the first column (on the far
left side) of the range you choose.
Wildcard matching in Excel is the ability to use characters which represent anything in a string of
text. For example, say you know someone's first name but not their last name. You could find this
person in an Excel spreadsheet by concatenating their first name with a wildcard character.
A question mark (?) replaces exactly one character in a string of text in Excel.
To use a wildcard in Excel, you concatenate an asterisk enclosed in quotes at the beginning or
end (or both) of your string of text. It looks like this:
=VLOOKUP("My text"&"*", A:C, 2, FALSE)
The VLOOKUP function will then return the first value it finds where the lookup_value is text that
begins with "My text". If our lookup_value was instead "*"&"my text", then it would search for any
text that ends with "my text" because the asterisk comes first.
A B C
Say we want to find the ID number of the employee whose first name is "Amy". We could use the
wildcard match as follows:
This will return the ID number of the first employee in the table whose name starts with the text
"Amy". Now suppose you want to find the ID of the employee whose middle initial is C. You can
use wildcard at the beginning and end of the search string as follows:
This will return the ID of the first employee who has "C." in their name.
COUNTIFS and COUNTIF
Of all Excel functions, COUNTIFS and COUNTIF are probably most often mixed up because they
look very much alike and both are purposed for counting cells based on the specified criteria.
The difference is that COUNTIF is designed for counting cells with a single condition in one range,
whereas COUNTIFS can evaluate different criteria in the same or in different ranges.
COUNTIF is an Excel function to count cells in a range that meet a single condition. `COUNTIF
can be used to count cells that contain dates, numbers, and text. The criteria used in COUNTIF
supports logical operators (>,<,<>,=) and wildcards (*,?) for partial matching.
Use COUNTIF, one of the statistical functions, to count the number of cells that meet a criterion;
Syntax
=COUNTIF(range, criteria)
Arguments
The COUNTIF function counts cells in a range that meet a given condition, referred to as criteria.
COUNTIF is a common, widely used function in Excel, and can be used to count cells that contain
dates, numbers, and text. Note that COUNTIF can only apply a single condition.
Applying criteria
The COUNTIF function supports logical operators (>,<,<>,<=,>=) and wildcards (*,?) for partial
matching. The tricky part about using the COUNTIF function is the syntax used to apply criteria.
COUNTIFS is in a group of eight functions that split logical criteria into two
parts, range and criteria. Because of this design, each condition requires a
separate range and criteria argument, and operators in the criteria must be enclosed in
double quotes (""). The table below shows examples of the syntax needed for common criteria:
In the worksheet shown above, the following formulas are used in cells G5, G6, and G7:
Notice COUNTIF is not case-sensitive, "CA" and "ca" are treated the same.
Double quotes ("") in criteria
In general, text values need to be enclosed in double quotes (""), and numbers do not.
However, when a logical operator is included with a number, the number and operator
must be enclosed in quotes, as seen in the second example below:
Not equal to
To construct "not equal to" criteria, use the "<>" operator surrounded by double quotes
(""). For example, the formula below will count cells not equal to "red" in the range
A1:A10:
Blank cells
COUNTIF can count cells that are blank or not blank. The formulas below count blank
and not blank cells in the range A1:A10:
Note: be aware that COUNTIF treats formulas that return an empty string ("") as not
blank. See this example for some workarounds to this problem.
Excel COUNTIFS function - syntax and usage
The Excel COUNTIFS function counts cells across multiple ranges based on one or several
conditions.
COUNTIFS syntax
• criteria_range1 (required) - defines the first range to which the first condition (criteria1)
shall be applied.
• criteria1 (required) - sets the condition in the form of a number, cell reference, text
string, expression or another Excel function. The criteria defines which cells shall be
counted and can be expressed as 10, "<=32", A6, "sweets".
• [criteria_range2, criteria2]… (optional) - these are additional ranges and their
associated criteria. You can specify up to 127 range/criteria pairs in your formulas.
Suppose you have a product list like shown in the screenshot below. You want to get a count of
items that are in stock (value in column B is greater than 0) but have not been sold yet (value is
column C is equal to 0).
=COUNTIFS(B2:B7,">0", C2:C7,"=0")
When you want to count items with identical criteria, you still need to supply each criteria_range
/ criteria pair individually.
For example, here's the right formula to count items that have 0 both in column B and column C:
=COUNTIFS($B$2:$B$7,"=0", $C$2:$C$7,"=0")
This COUNTIFS formula returns 1 because only "Grapes" have "0" value in both columns.
Using a simpler formula with a single criteria_range like COUNTIFS(B2:C7,"=0") would yield a
different result - the total count of cells in the range B2:C7 containing a zero (which is 4 in this
example).
Using a simpler formula with a single criteria_range like COUNTIFS(B2:C7,"=0") would yield a
different result - the total count of cells in the range B2:C7 containing a zero (which is 4 in this
example).
In the table below, supposing you want to count orders with the "Cancelled" and
"Pending" status. To have it doen, you can simply write 2 regular Countif formulas and
add up the results:
=COUNTIF($C$2:$C$11,"Cancelled") + COUNTIF($C$2:$C$11,"Pending")
In case each of the functions is supposed to evaluate more than one condition, use
COUNTIFS instead of COUNTIF. For example, to get the count of "Cancelled" and
"Pending" orders for "Apples" use this formula:
In situations when you have to evaluate a lot of criteria, the above approach is not the
best way to go because your formula would grow too big in size. To perform the same
calculations in a more compact formula, list all of your criteria in an array constant, and
supply that array to the criteria argument of the COUNTIFS function. To get the total
count, embed COUNTIFS inside the SUM function, like this:
SUM(COUNTIFS(range,{"criteria1","criteria2","criteria3",…}))
In our sample table, to count orders with the status "Cancelled" or "Pending" or "In
transit", the formula would go as follows:
=SUM(COUNTIFS($A$2:$A$11,"apples",$C$2:$C$11,{"cancelled","pending","in transit"}))
To find out how many numbers between 5 and 10 (not including 5 and 10) are contained in cells
C2 through C10, use this formula:
=COUNTIFS(C2:C10,">5", C2:C10,"<10")
To include 5 and 10 in the count, use the "greater than or equal to" and "less than or equal to"
operators:
=COUNTIFS(B2:B10,">=5", B2:B10,"<=10")
How to use COUNTIFS with wildcard characters
In Excel COUNTIFS formulas, you can use the following wildcard characters:
• Question mark (?) - matches any single character, use it to count cells starting
and/or ending with certain characters.
• Asterisk (*) - matches any sequence of characters, you use it to count cells
containing a specified word or a character(s) as part of the cell's contents.
Tip. If you want to count cells with an actual question mark or asterisk, type a tilde (~)
before an asterisk or question mark.
The COUNTIFS and COUNTIF formulas you use for dates are very much similar to the above
formulas for numbers.
To count the dates that fall in a certain date range, you can also use either a COUNTIFS
formula with two criteria or a combination of two COUNTIF functions.
For example, the following formulas count the number of dates in cells C2 through C10
that fall between 1-Jun-2014 and 7-Jun-2014, inclusive:
In the same manner, you can use a COUNTIFS formula to count the number of
dates in different columns that meet 2 or more conditions. For instance, the below
formula will find out how many products were purchased after the 20th of May and
delivered after the 1st of June:
Example 3. Count dates with multiple conditions based on the current date
You can use Excel's TODAY() function in combination with COUNTIF to count dates based on
the current date.
For example, the following COUNTIF formula with two ranges and two criteria will tell you how
many products have already been purchased but not delivered yet.
SUMIF Function
The Excel SUMIF function returns the sum of cells that meet a single condition. Criteria can be
applied to dates, numbers, and text. The SUMIF function supports logical operators (>,<,<>,=)
and wildcards (*,?) for partial matching.
Syntax
=SUMIF(range, criteria, [sum_range])
Arguments
The SUMIF function takes three arguments. The first argument, range, is the range of cells to
apply criteria to. The second argument, criteria, is the criteria to apply, along with any logical
operators. The last argument, sum_range, is the range that should be summed. Note
that sum_range is optional. If sum_range is not provided, SUMIF will sum cells in the first
argument, range.
Worksheet example
In the worksheet shown, there are three SUMIF formulas. In the first formula (G5),
SUMIF returns total Sales where Name = "jim". In the second formula (G6), SUMIF
returns total Sales where State = "ca" (California). In the third formula (G7), SUMIF
returns the total of Sales > 100:
Notice the equals sign (=) is not required when constructing "is equal to" criteria. Also
notice SUMIF is not case-sensitive; you can use "jim" or "Jim". Finally, notice that the
last formula does not include sum_range, so range is summed instead.
Criteria in another cell
A value from another cell can be included in criteria using concatenation. In the
example below, SUMIF will return the sum of all sales over the value in G4. Notice the
greater than operator (>), which is text, must be enclosed in quotes. The formula in G5
is:
Not equal to
To express "not equal to" criteria, use the "<>" operator surrounded by double quotes
(""):
Blank cells
SUMIF can calculate sums based on cells that are blank or not blank. In the example
below, SUMIF is used to sum the amounts in column C depending on whether column
D contains "x" or is empty:
=SUMIF(D5:D9,"",C5:C9) // blank
=SUMIF(D5:D9,"<>",C5:C9) // not blank