EXCEL VBA or Function VLoopup - Based on Multiple Criteria to find the value - excel

I want to Vlookup using two criteria:
the value in Column D;
The amount in Column A and Column G which can be approximately matched.
The normal Vlookup only returns the value of the first row, so one solution I think about is to match both the Invoice Number and Approximately Match the amount.
I am not sure if it is the best solution, please advise a better one, not necessarily in VBA, it can also use INDEX or MATCH functions, etc.
Thanks a lot in advance!
Below is the formula I've been trying, it is like something If true, then this value, if false then Lookup the next one.... But I am still thinking about how to navigate though the Vlookup to search next value, do I need to use VBA to solve this?! Vlookup based on Multiple Criteria2

The following formula should work:
=INDEX(G3:G5,SUMPRODUCT(MAX((D3:D5=B3)*ROW(D3:D5)))-3,1)
That's just a fancier version of Index(Match) that is used as a common replacement for vlookup. In this case, instead of Match we use Sumproduct to get the max row with the criteria we are looking for.
ADDED: Added a -3 to the second paramater of INDEX in order to adjust the Row() returned to the actual range that is being Indexed.

Related

Excel CountIfs with an or condition on Dates

Looking to try and find the following in Excel but am having issues getting this to work.
I have Dates in Column A which need to be checked against Dates in the Query Column to compare. I want to count all records where the comparison column is greater than the date in A5 or is an empty cell. There are other conditions that I will also want to check but cannot seem to get this to work.
=COUNTIFS(Query1[Purchased Date],OR(Query1[Purchased Date]="",Query1[Purchased Date]>A5))
use:
=SUMPRODUCT(--((Query1[Purchased Date]="")+(Query1[Purchased Date]>A5)>0))
You can use an array function instead of countif.
Suppose your dates are in the range A2:A25 and your reference date is in cell B2.
If you type in any other cell
=SUM((A2:A25>B2)+(A2:A25=""))
and hit Ctrl+Shift+Enter it will give you the count you want.
This happens because Excel will resolve the first inner parentheses (A2:A25>B2) as an array of TRUE/FALSE, and does the same to the second (A2:A25=""). Next it will sum them, which is equivalent to the OR operation, and yields as a result an array of zeros (FALSE) and ones (TRUE). It wraps up by summing this array (with the function sum), all in one cell.
Perhaps this is more of a comment than an answer, but in this particular case you can just add the two separate totals:
=COUNTIF(Query1[Purchased Date],"")+COUNTIF(Query1[Purchased Date],">"&A5)
because the conditions are mutually exclusive.
Whether this helps or not depends on what additional criteria you want to add.
BTW there are two issues with your original formula:
(1) If you are combining lists of values with OR logic, you have to use addition instead (as in the two answers which use Sum or Sumproduct).
(2) The syntax of a Countif or Countifs where the range has to be kept separate from the criteria won't let you do what you want to do, so this again leads you to Sum or Sumproduct.

Using COUNTIFS in Excel, check cells (containing formulas) that are empty

Good day.
In it's basic form, I need to count how many cells are empty.
Using the following below, I can count how many cells are empty.
=COUNTIF(Sheet1!C:C,"<>")
However, if the cells in column C contain formulas, it won't work.
After some googling, I found out that using SUMPRODUCT will get what I need
=SUMPRODUCT(--(LEN(Sheet1!C:C)>0))
Now, here's my problem.
I need to use that as a criteria inside a COUNTIFS function, but I don't know how to do that because it's referencing some ranges.
So just to make it simple, using COUNTIF or COUNTIFS function specifically, how can I pass a criteria that checks if cell (with formula) is empty.
This formula returns 0 but most likely I'm just not passing it properly as a criteria.
=COUNTIF(Sheet1!C:C,SUMPRODUCT(--(LEN(Sheet1!C:C)>0)))
Happy for other ways to count cells (with formulas) which are empty, but I need to use it as a criteria for a COUNTIF/COUNTIFS function.
Thank you very much.
If I understand what you're looking for, you were pretty close already.
Following the formula's you already found you can use them like:
=COUNTIF(Sheet1!C:C,"<>")-SUMPRODUCT(--(LEN(Sheet1!C:C)>0))
It will result in the count of cells that have a value/formula minus the count of cells that show a value (blank formula result is excluded).
The result is the count of cells that contain a formula with blank result.

Excel - VLOOKUP vs. INDEX/MATCH - Which is better?

I understand how to use each method: VLOOKUP (or HLOOKUP) vs. INDEX/MATCH.
I'm looking for differences between them not in terms of personal preference, but primarily in the following areas:
Is there something that one method can do that the other cannot?
Which one is more efficient in general (or does it depend on the situation)?
Any other advantages/disadvantages to using one method vs. the other
NOTE: I am answering my own question here but looking to see if anyone else has other insights I hadn't thought of.
I prefer to use INDEX/MATCH in practically every situation because it is far more flexible and has the potential to be much more efficient depending on how large the lookup table is.
The only time when I can really justify using VLOOKUP is for very straight-forward tables where the column index number is dynamic, although even in this case, INDEX/MATCH is equally viable.
I'll give a few specific examples below to demonstrate the detailed differences between the two methods.
INDEX/MATCH can lookup to the left (or anywhere else you want)
This is probably the most obvious advantages to INDEX/MATCH as well as one of the biggest downfalls of VLOOKUP. VLOOKUP can only lookup to the right, INDEX/MATCH can lookup from any range, including different sheets if necessary.
The example below cannot be accomplished with VLOOKUP.
INDEX/MATCH has the potential to use smaller cell ranges (thus increasing efficiency)
Consider the example below. It can be accomplished with either method.
Both of these formulas work fine. However, since the VLOOKUP formula contains a larger range than the INDEX/MATCH formula, it is unnecessarily volatile.
If any cell in the range B1:G4 changes, the VLOOKUP formula must recalculate (because B1:G4 is within the range A1:H4) even though changing any cell in B1:G4 will not affect the outcome of the formula. This is not an issue for INDEX/MATCH because its formula does not contain the range B1:G4.
Using VLOOKUP with fixed col_index_number is dangerous
The main issue I see with having a fixed column index number is that it will not update as it should if full columns are inserted. Consider the following example:
This formula works fine unless a column is inserted within the lookup table. In that case, the formula will lookup the value to the left of where it should. See below, result after a column has been inserted.
This can actually be alleviated by using the following VLOOKUP formula instead:
= VLOOKUP("s",A1:H4,COLUMN(H1)-COLUMN(A1)+1,FALSE)
Now H1 will automatically update to I1 if a column is inserted, thus preserving the reference to the same column. However, this is entirely unnecessary because INDEX/MATCH can accomplish this without this problem with the formula below.
= INDEX(H1:H4,MATCH("s",A1:A4,0))
I realize this is an unlikely scenario, but it always bothered me that VLOOKUP by default looks up based on a fixed column index that does not automatically update if columns are inserted. To me, it just seems to make the VLOOKUP function more fragile.
INDEX/MATCH can handle variable column indexes just as well, but longer formula
If the column index number itself is dynamic, this is really the only case when I think VLOOKUP simplifies things a bit, but again the INDEX/MATCH alternative is just as good, just slightly more confusing. See below examples.
INDEX/MATCH is more efficient for multiple lookups
(thanks to #jeffreyweir)
If multiple lookup values are needed for a single match value, it is much more efficient to have a helper cell with the match value. This way, the match only has to be computed once, instead of one for each lookup formula. See example below.
This match value can then be used to return the appropriate lookup values. See example below, (formula has been dragged to the right).
This manual "splitting" of the match value and index values is not an option with VLOOKUP since the match value is an "internal" variable in VLOOKUP and cannot be accessed.
INDEX/MATCH can look up a range, allowing another operation
Let's say for example you want to find a max value in a column based on the column name.
You can first use MATCH to find the appropriate column, then INDEX to return the range of that entire column, then use MAX to find the max of that range.
See example below, the formula in H4 looks up the max value of the column name specified in cell G4. This cannot be accomplished using VLOOKUP alone.
MATCH doesn't have to match an exact value
Usually MATCH is used with the third argument as 0, meaning "find an exact match". But depending on the situation, using -1 or 1 as the third argument of MATCH can be very useful.
For example, the following formula returns the row number of the last row in column A that contains a number:
= MATCH(-1E+300,A:A,-1)
This is because this formula starts from the bottom of the A column and works its way toward the top, and returns the first row number in the A column where the value is greater than or equal to -1E+300 (which is basically any number).
Then INDEX can be used in combination with this to return the value in that cell. See example below.
In Summary
VLOOKUP is, at best, as good as INDEX/MATCH and admittedly slightly less confusing in some situations. And at worst, VLOOKUP is much more unsafe and volatile than INDEX/MATCH.
Also worth noting that if you want to look up a range instead of a single value, INDEX/MATCH must be used. VLOOKUP cannot be used to look up a range.
For these reasons, I generally prefer INDEX/MATCH in practically all situations.

#REF error when using INDEX function

I cannot understand why everytime I use INDEX in excel to find a value given the two criteria, I get a #REF error.
INDEX(C2:L1048576,MATCH(O1,A2:A1048576,0),MATCH(O2,B2:B1048576,0))
There were no deleted cells, nor were they shifted at any point in time. They have the same number of rows too. The arrays to search into are correct.
Thanks. I would appreciate if anyone can give me some guidance. I am new to the INDEX formula.
The formula you're using doesn't find a value according to two criteria. The comments you were given explain what you're actually doing.
INDEX returns one cell value from a given range, according to a row and column index - the location within your range, starting with 1. (If your reference has only one row or column, one of them can be omitted).
MATCH finds a value in a range and returns its index.
So finding one value in a one-dimension range is easy using these two functions, using something like this (with a range of one column and multiple rows) =INDEX(range,MATCH(value,range,0),1).
To find two criteria you need to tweak this concept. One way is to use concatenation of strings, using the & operator, and for this you'll also need to use an array formula (entering it using Ctrl+Shift+Enter) like this formula:
=INDEX($C$2:$C$1048576,MATCH(O1&O2,$A$2:$A$1048576&$B$2:$B$1048576,0),1)
It's not clear what you are trying to return, so this formula will return the corresponding value in column C. You can use this concept to return each value from the rest of the columns D:L, one by one, or concatenate them.

how to count multiple columns using countifs

I am putting in a formula to count the number of times a quote is required Indicated by the letter Q in a given column, when I put the formula in for one column I get the correct answer, but when I want to do it for multiple columns I get zero, can anyone help please?
the formula is
=COUNTIFS(D10:D29,"=Q",G10:G29,"=Q")
Try either
=SUMPRODUCT((D10:D29="Q")+(G10:G29="Q"))
or
=SUMPRODUCT(((D10:D29="Q")+(G10:G29="Q")>0)+0)
the former will count 2 if you have Qs in both D10 and G10 - the latter only counts each row once at most, even if there are two "Q"s
countifs criteria are connected by a logical AND. so that formula is saying it must find your string in column D AND in column G. Apparently there are 0 instances of that. if you want the total number of cells with it then make it one range.
If the must be non-contiguous, use multiple countif formulas and add them
as a note, here I would change my formula back to countif, instead of countifs for backwards compatibility since I don't use the extra criteria.
EDIT: my second example was incorrect (See comments) so I removed it
Actually, what I've found is that there is a way better way instead of the sumproduct, which can result in a overly-long formula if you have 5 columns. Instead, I found that using the SUM+IF function as we use the SUMPRODUCT, will achieve the result faster and better.
=SUM(IF((E:I="ABC")*(B:B="DEF"); 1; 0))
This function returns the number of rows that contain both "ABC" and "DEF" in the defined columns.
Spread the word!

Resources