Relative cell position across worksheets with IF statement - excel

I have an Excel worksheet (Sheet 1) with a list of People's named and their responsible training groups... basically numbers assigned to different handlers.
I need to:
On Sheet 2, I need an IF statement that checks Sheet 1, Column B and if a certain person's name appears, it returns the value to the immediate left.
I have something like this on Sheet 1:
Group Name
101 Fred
102 Wilma
103 Fred
104 Fred
105 Barney
106 Wilma
On Sheet 2, I need this:
Barney Fred Wilma
105 101 102
&nbsp &nbsp103 106
&nbsp104
The equation I am trying to use in each cell on sheet 2 is"
=IF('Sheet 1'!B:B="Barney",relative position one cell to the left of name,"")
Thanks in advance.

No array formula needed:
MINIFS($A$2:$A$7,$B$2:$B$7,D$1,$A$2:$A$7,">" & MAX(D$1:D1))
Just a simple MINIFS. Then wrap it in an IF to remove the zeros:
EDIT: Further clarification in response to comment.
I have put both tables on the same sheet for ease, however, one could easily be moved onto a separate sheet if needed.
The formula works by selecting the group with the lowest value, which has the name which appears at the top of the column AND which has not already been listed.
So:
MINIFS($A$2:$A$7,$B$2:$B$7,D$1,$A$2:$A$7,">" & MAX(D$1:D1))
The first argument in the MINIFS formula is $A$2:$A$7. This is the column containing all the groups, which we want to split out and list against each name.
The 2nd and 3rd argument ($B$2:$B$7,D$1) is a criteria range and it's value. This is used to filter out any name we dont want to return in this particular cell (any that are not "Barney" in the case of D2). Notice that the D$1 in the formula does not start with a $, this means that we can just drag the formula across the other name columns and it will filter out the correct names automatically.
The 4th and 5th Arguments ($A$2:$A$7,">" & MAX(D$1:D1)) are also a criteria range and its value. Howevever, in the case of the value this time, we are looking for the max value within a range. We are looking at all the groups which have already been listed against a name and using MAX to select the highest one.Notice that there is a $ before the first "1" in the formula but not before the second. This means that as we drag the formula down, the range that it is looking for the MAX in, will automatically expand to include the cell just above the current one. Also note that there is no $before either of the "D"s, this is so that when you drag the formula to the right, the range it is checking also moves to the right. Once we have the MAX group which has already been listed against a name, we tell the MINIFS formula to only look at groups which are greater than that.
Once there are no groups for a name which are greater than the previously listed group, the MINIFS formula will return 0. To prevent the table from showing lot's of 0's, we simply say "If the MINIFS formula returns 0, then return an empty string instead:
=IF(<the result of the MINIFS>=0, "",<the result of the MINIFS>)
So the final formula would be:
=IF(MINIFS($A$2:$A$7,$B$2:$B$7,D$1,$A$2:$A$7,">" & MAX(D$1:D1))=0,"",MINIFS($A$2:$A$7,$B$2:$B$7,D$1,$A$2:$A$7,">" & MAX(D$1:D1)))
I hope that makes more sense for you. I am sorry if the explanation is a bit long winded, but as you didn't give me a specific area that you didn't understand, I just tried to expand my explanation generally. If there is any specific part you need me to explain further, please let me know.

Related

SumIF Using Table/Named Range Instead of Single Cell Criteria

I have 2 sheets in a workbook (Sheet1, Sheet2).
Sheet 2 contains a table (Named Table1) with 5 columns:
Takeaways
Household
Clothing
Fuel
Groceries
On sheet one, I have 2 columns:
Expense Name
Expense Total
Now, what I am trying to do is:
Set the range for the Expense Name (Range 1)
Set the range for the Expense Total (Range 2)
Compare Range 1 with the respective column in the table and only add up the values for matches
For example, in Range 1 (B6:B16):
BP
Caltex
McDonalds
KFC
In Range 2 (C6:C16):
300
400
200
150
Now, all I want to do is add up the values for the Takeaways (McDonalds, KFC) and exclude anything that DOES NOT match the criteria.
So my sum total will be all occurrences of Takeaways - provided they are listed in my table - 350 in this case.
But I cannot seem to get the formula to work.
I used these sources:
https://exceljet.net/excel-functions/excel-sumifs-function
Selecting a Specific Column of a Named Range for the SUMIF Function
and ended up with this formula:
=SUMIF($B$6:$B$16;Table1[Takeaways];C6:C16)
This source:
https://excelchamps.com/blog/sumif-sumifs-or-logic/
and ended up with this formula:
=SUM(SUMIFS(C6:C16;B6:B16;Table1[Takeaways]))
Both formulae return 0.
BUT, with BOTH of them, if I change Table1[Takeaways] to "McDonalds", then it correctly identifies every occurrence of the word "McDonalds" in Range 1.
EDIT:
I have updated the formulae above to match the images below.
This is the table that contains the references:
This table contains the data:
Formula:
Cell C4 (Next to Takeaways): =SUMIF($B$6:B$16;Table1[Takeaways];C6:C16)
Cell C5 (Next to Fuel): =SUM(SUMIFS(C6:C16;B6:B16;Table1[Fuel]))
It appears that ONLY BP is being detected in the formula.
This is a an output table when I use the formulae with a single cell reference and not a table or used range:
Formula:
Cell F4 (Next to BP): =SUMIF($B$6:B$16;"BP";C6:C16)
Cell F5 (Next to Caltex): =SUM(SUMIFS(C6:C16;B6:B16;"Caltex"))
Cell F6 (Next to McDonalds): =SUMIF($B$6:B$16;"McDonalds";C6:C16)
Cell F7 (Next to KFC): =SUM(SUMIFS(C6:C16;B6:B16;"KFC"))
If I understand correctly what you're trying to achieve, I think your setup is not right conceptually.
It looks like you're trying to track expenses, and each expense (or payee) is allocated to a category ("Takeaways", "Household" etc.). From a relational-model point of view, your second table (which defines the category for each expense/payee) should only have two columns (or variables): Expense Name and Expense Category.
The table you set up ('Sheet 2') uses the categories (i.e., possible values) as different columns (i.e., variables). But there's only variable, namely the "Expense Category", and the categories themselves are the possible values.
If you set it up like that, the problem changes: you can add a dependent column to your first table that shows the category for each payee (or "Expense Name"), using a VLOOKUP() from the second table.
You can then sum the expenses for all payees matching that category.
Note: I've created the illustration using LibreOffice Calc, so there might be some small differences, but the logic is the same.
Without seeing the data in L and K I can't give you a full answer - but likely it's to do with the way you're pulling your Array
Try something similar to this
=SUMPRODUCT(SUMIFS($L$11:$L$43,$K$11:$K$43,CHOOSE({1,2},Takeaways,"anything else you wanted to sum")))
Remember SUMIFS is for multiple criteria, so if you're only calculating one, you'll need =SUMPRODUCT(SUMIF(
The way the above works is with vertical vectors only, but changing your named ranges so the table of 2 columns is 2 named ranges instead should be okay - unless it's part of your requirements
Table 2 would become expense_Name and expense_Total etc
I was about to close this as a duplicate of my own question here but there is a bit of a difference in using a named range I think. However the logic behind this follows more or less the same approach.
Working further on my partial solution below I derived the following formula:
=SUMPRODUCT(COUNTIF(Table1[Takeaways];Range1)*Range2)
The COUNTIF() part counts the number of occurrences of the cell value in your table. Therefore make sure there are no duplicates in your table. If the value is present in the table the result of COUNTIF() will be 0. This way we create a matrix of 1's and 0's. By multiplying and the use of SUMPRODUCT() we force excel to perform matrix calculations and return the correct result.
Partial solution
I used the following formula:
=SUMPRODUCT(ISNUMBER(MATCH(Range1;Table1[Takeaways]))*Range2)
The formula does the following:
The MATCH()checks if the value in Range1 is present in your table and returns the position of the matching value in your table.
The ISNUMBER() checks if a match is found by checking if the MATCH() fucntion returned a number
Multiplying this with Range2 forces matrix calculation, using the SUMPRODUCT() function
EDIT:
This worked for a really limited sample. As soon as I added the fourth row to my data the formula stopped working as intended. See screenshot:
It took the first two values into the sum correctly, the fourth is not taken into account.

Excel formula to lookup the last value in a column and return the value of the adjacent cell

I have the following formula to return the value of the last value in a column:
=LOOKUP(2,1/(D:D<>""),D:D)
What I need now is to return the value of the cell adjacent to it as well. (It will not necessarily be the last value in that column and the info in Column D could have duplicates.
If your data looks like this:
A 1
A 2
A 3
B 4
B 5
B 6
C 7
To get last value this will do the trick:
=INDIRECT("B"&COUNTA(A:A))
And to get last where value is A:
=INDIRECT("B"&MATCH("A",A1:A7,0)+COUNTIF(A1:A7,"A")-1)
Just use next column:
=LOOKUP(2,1/(D:D<>""),E:E)
Ok, So I have found an answer by playing around with array formulas.
The problem was that this is a stock control sheet where there are changes made at multiple times, each recorded in the next available row. There is always a date (Column E) but not necessarily a Supplier, as it might be stock moving out. When a Supplier delivers, the Supplier name is recorded in Column D. In D1 the last supplier is then shown with the following formula.
=LOOKUP(2,1/(D:D<>""),D:D)
I want to then see what date it was last received. The formula I found that works is as follows (Array Formula):
=INDEX(E:E,MAX(IF(D:D=D1,ROW(D:D)-ROW(INDEX(D:D,1,1))+1)))
This is generally how I do it:
=XMATCH(FALSE,ISBLANK(A:A),0,-1)
This is what each part does:
Parameter
Explanation
FALSE
Instructs Excel to find the first instance of FALSE that it finds
ISBLANK(A:A)
Takes in the column A:A and notionally assigns a value to every item in the column
0
Means we want an exact match. Probably not necessary to put in, but I think it's good practice anyway
-1
Instructs Excel to start the search at the bottom/right of the range and work up/left. If you change this to 1 (the default), Excel will begin the search at the top/left and work down/right
So, taken together, this will search from the bottom of the column A:A, until Excel finds the first cell that is not blank, and return that cell.
Also, yes, this equation can be changed to a row format (e.g. 1:1), and can take a smaller range (e.g. A1:A20), but it cannot take a 2-dimensional range (e.g. A1:B20).
As a practical matter, this approach is much faster than other approaches (and much faster than you'd think, given it's evaluating against every row/column in the range), and won't get fooled by columns that have empty spaces in them (like with a COUNTA style approach).

Excel Index Return multiple values matching multiple criteria

Index and Match wont return multiple values need to find a way, whether thats a function or VBA.
Essentially I have an Index and Match function that looks at two variables and then returns the corresponding Company that matches from a different sheet.
i.e you select "Australia" from a drop down and then "GOLD" from another drop down then the Index and Match will pull a company from another sheet that operates in Australia and mines Gold.
So far the function works but It only returns the first result and there may be more than one company and I haven't figured out how to return more than one result.
The code so far is as follows:
=INDEX(Datapage!B$2:B$280,MATCH(1,(A$8=Datapage!$C$2:$C$280) * (B$8=Sheet11!$D$2:$D$280),0))
I have also tried this one:
=IFERROR(INDEX(Table26[[Location]:[Commodity]],SMALL(IF(Table26[Location]=$B8,ROW(Table26[Commodity])-ROW($D$2)+1),COLUMNS($B14:B14))),"")
the Data sheet has values in B2 - D280
But nothing happens and it only searches off one variable which is useless for this need.
B being Company name
C being Location
D being Commodity
I've attached some pictures for clarification
this is what the sheets look like
Any help would be greatly appreciated!
Thanks
For anyone interested I figured out a function that doesn't require any VBA.
=IFERROR(INDEX(Data!$B$2:$B$280, SMALL(IF($A$8&" "&$B$8=Data!$A$2:$A$280, ROW(Data!$B$2:$B$280)-1,""), ROW()-14)),"")
The first data in B column is the data I want returned if a match is found.
The "Small(IF(A..." function is combining the two variables I want a match looked for. Ie. A=Australia & B=GOLD etc etc and it'll look for a match on the next sheet.
The "ROW(Data..." compares the value in cell A8 and B8 with each value in the range. If a match is found, "ROW(Data!$B$2:$B$280)-1" returns the number of the corresponding row (-1 is used to deduct the header row). If the compared values do not match, the IF function returns an empty string.
The "ROW()-14)),""" function acts as an incremental counter. Since the formula is copied into cells A15, we add -14 for the function to return 1 for cell A15 (row 15 minus 14), 15 for cell A16 (row 16 minus 15), etc

Ms Excel 2010 Comparing 2 columns and if the results match then compare another 2 and return the if they match

Ok this is a little confusing to explain, and even worse to word and search the internet for.
I have 4 columns with 700 rows in each, 2 of the columns are for IE8 that are GPO name, and Value, the other 2 are For IE10 that are GPO Name and Value.
I have managed to find a formula to compare the GPO names present in each column, and here is the formula:
=IFERROR(INDEX($A$1:$A$700,MATCH(B2,$A$1:$A$700,0),1),"*** NO ***")
So if the value is found in the other column it will show the name, otherwise it will display as * NO *.
So now, using this formula, i want to compare the values, but the problem is i want to compare them only if the name are present in both. Another problem is the Value might be "Enabled", but there are 400 other "Enabled" in the column, so it has to correlate to the value to the right of the GPO Name.
I cant attach a copy of the excel spreadsheet, but hopefully someone will have some idea of how this could be achieved.
Thanks in advance.
AntonSK
In your formula:
=IFERROR(INDEX($A$1:$A$700,MATCH(B2,$A$1:$A$700,0),1),"*** NO ***")
MATCH() will return the ROW NUMBER from the range A1:A700, where the value matches B2. Lets say this is 32.
INNDEX() will then get the VALUE of the cell in row 32 of your speciffied range: A1:A700. letss say this is "GPO_Name_32".
You can use the result from the MATCH() to return a value from a different range, in the corresponding row (for example an adjacent column). Therefore your formulas would be (something like):
v -
=IFERROR(INDEX($A$1:$A$700,MATCH(B2,$A$1:$A$700,0),1),"*** NO ***")
and
=IFERROR(INDEX($C$1:$C$700,MATCH(B2,$A$1:$A$700,0),1),"*** NO ***")

Find something in column A then show the value of B for that row in Excel 2010

Basically my problem is that I have a string in one cell in excel, I then need to see if that string exists in another row (not one cell but the whole row) and if so then print the contents of another cell in the same row but in another column.
I will give a basic example:
Title Answer
Police 15
Ambulance 20
Fire 89
Now I need to scan the title column for, say, "Police" and then populate the cell with the value under Answer (in this case 15).
I cant just say IF(A2="Police";B2;"" as I need the scan the whole of the Title column.
I have tried using IF(COUNTIF(A$2:A$100;"Police"); which scans the contents of A2 to A100 for the string Police, and know how to make it print a constant (just put something after the ;) but cant work out how to make that "constant" a variable that changes depending on the found row. So if the COUNTIF found Police in cell A44 then the answer to my formula would be B44, the same as if it found Police in A62 then my formula should show B62
I hope this makes sense and that someone can help me :)
Note that I am using excel 2010 and need a normal formula as I can not use scripting for this document.
EDIT:
Here is what I have so far, note that the spreadsheet I am using is far more complex than the "simple" example I have in the question...
=IF(ISNUMBER(FIND("RuhrP";F9));LOOKUP(A9;Ruhrpumpen!A$5:A$100;Ruhrpumpen!I$5:I$100);"")
This is showing "RuhrP" in every answer where "RuhrP" is found in F9 and not the answer I want which should be that found in RuhrPumpen!I$5:I$100 where the cell index is the same as that for the A coloum where A9 was found. Again, sorry for the complexity I cant think of any better way to word it.
I note you suggested this formula
=IF(ISNUMBER(FIND("RuhrP";F9));LOOKUP(A9;Ruhrpumpen!A$5:A$100;Ruhrpumpen!I$5:I$100);"")
.....but LOOKUP isn't appropriate here because I assume you want an exact match (LOOKUP won't guarantee that and also data in lookup range has to be sorted), so VLOOKUP or INDEX/MATCH would be better....and you can also use IFERROR to avoid the IF function, i.e
=IFERROR(VLOOKUP(A9;Ruhrpumpen!A$5:Z$100;9;0);"")
Note: VLOOKUP always looks up the lookup value (A9) in the first column of the "table array" and returns a value from the nth column of the "table array" where n is defined by col_index_num, in this case 9
INDEX/MATCH is sometimes more flexible because you can explicitly define the lookup column and the return column (and return column can be to the left of the lookup column which can't be the case in VLOOKUP), so that would look like this:
=IFERROR(INDEX(Ruhrpumpen!I$5:I$100;MATCH(A9;Ruhrpumpen!A$5:A$100;0));"")
INDEX/MATCH also allows you to more easily return multiple values from different columns, e.g. by using $ signs in front of A9 and the lookup range Ruhrpumpen!A$5:A$100, i.e.
=IFERROR(INDEX(Ruhrpumpen!I$5:I$100;MATCH($A9;Ruhrpumpen!$A$5:$A$100;0));"")
this version can be dragged across to get successive values from column I, column J, column K etc.....
Assuming
source data range is A1:B100.
query cell is D1 (here you will input Police or Fire).
result cell is E1
Formula in E1 = VLOOKUP(D1, A1:B100, 2, FALSE)
I figured out such data design:
Main sheet:
Column A: Pump codes (numbers)
Column B: formula showing a corresponding row in sheet 'Ruhrpumpen'
=ROW(Pump_codes)+MATCH(A2;Ruhrpumpen!$I$5:$I$100;0)
Formulae have ";" instead of ",", it should be also German notation. If not, pleace replace.
Column C: formula showing data in 'Ruhrpumpen' column A from a row found by formula in col B
=INDIRECT("Ruhrpumpen!A"&$B2)
Column D: formula showing data in 'Ruhrpumpen' column B from a row found by formula in col B:
=INDIRECT("Ruhrpumpen!B"&$B2)
Sheet 'Ruhrpumpen':
Column A: some data about a certain pump
Column B: some more data
Column I: pump codes. Beginning of the list includes defined name 'Pump_codes' used by the formula in column B of the main sheet.
Spreadsheet example: http://www.bumpclub.ee/~jyri_r/Excel/Data_from_other_sheet_by_code_row.xls
Guys Its very interesting to know that many of us face the problem of replication of lookup value while using the Vlookup/Index with Match or Hlookup.... If we have duplicate value in a cell we all know, Vlookup will pick up against the first item would be matching in loopkup array....So here is solution for you all...
e.g.
in Column A we have field called company....
Column A Column B Column C
Company_Name Value
Monster 25000
Naukri 30000
WNS 80000
American Express 40000
Bank of America 50000
Alcatel Lucent 35000
Google 75000
Microsoft 60000
Monster 35000
Bank of America 15000
Now if you lookup the above dataset, you would see the duplicity is in Company Name at Row No# 10 & 11. So if you put the vlookup, the data will be picking up which comes first..But if you use the below formula, you can make your lookup value Unique and can pick any data easily without having any dispute or facing any problem
Put the formula in C2.........A2&"_"&COUNTIF(A2:$A$2,A2)..........Result will be Monster_1 for first line item and for row no 10 & 11.....Monster_2, Bank of America_2 respectively....Here you go now you have the unique value so now you can pick any data easily now..
Cheers!!!
Anil Dhawan

Resources