Reading Column Value based on Row number - Excel - excel-formula

i am new to excel formulas,
I have the below scenarios, there are 2 sheets in my workbook the first sheet is:
ProjectName Primary Secondary
P1 Ajay Shekar
P2 Rahul Ajay
P3 Shekar Rahul
P4 Raj Simran
the second sheet is resource Specific let say for Ajay and has a drop down based on ProjectName.
My requirement is When I select the Project name say P1, the second column should populate with the Value 'Primary', if its P2 the value should be 'Secondary' in the Sheet2.
Can anyone Please suggest how can i achieve this solution?
I tried getting Row Number for Matching Project Name but i am not able to get the relvant column number based on Row number.

=IF(VLOOKUP(B1,'Resource Sheeet'!A:C,2,FALSE)=A1,"Primary",IF(VLOOKUP(B1,'Resource Sheeet'!A:C,3,FALSE)=A1,"Secondary",NA()))
Resource Name is in cell A1, Project Name is in cell B1. Just runs 2 VLOOKUPS to see if the name exists in column B or C. Only works if there is only 1 row per project!
If you have more than one row for each project, you could try this:
=IF(COUNTIFS('Resource Sheet'!A:A,B1,'Resource Sheet'!B:B,A1)>0,COUNTIFS('Resource Sheet'!A:A,B1,'Resource Sheet'!B:B,A1) & " Primary","") & IF(--COUNTIFS('Resource Sheet'!A:A,B1,'Resource Sheet'!B:B,A1)*--COUNTIFS('Resource Sheet'!A:A,B1,'Resource Sheet'!C:C,A1)," and ","") & IF(COUNTIFS('Resource Sheet'!A:A,B1,'Resource Sheet'!C:C,A1)>0,COUNTIFS('Resource Sheet'!A:A,B1,'Resource Sheet'!C:C,A1) & " Secondary","")
Which will list out how many Primary/Seconday matches for the project (e.g. "1 Primary", "1 Primary and 1 Secondary", "3 Secondary", etc)
{EDIT} Based on comment below: This formula will match for any number of columns, but still only works for 1 row per project:
=INDEX('Resource Sheet'!$1:$1,1,MATCH(A1,OFFSET('Resource Sheet'!$A$1,SUMPRODUCT(MAX(ROW('Resource Sheet'!$A:$A)*--('Resource Sheet'!$A:$A=B1)))-1,0,1,COUNTA('Resource Sheet'!1:1)+COUNTBLANK('Resource Sheet'!1:1)),0))
INDEX('Resource Sheet'!$1:$1,1,MATCH(..)) : This will provide the Header value from Row 1 of the Resource Sheet in the Column number provided by the MATCH
That's the simple bit. Now, MATCH is also fairly simple - give it a value (A1) and a Row or Column, set the Match-type to 0, and it will give you which Column/Row in the Row/Column matches the value.
So, we look along the row for the Project. Do do that, we will use OFFSET to make the row-range:
OFFSET('Resource Sheet'!$A$1,SUMPRODUCT(MAX(ROW('Resource Sheet'!$A:$A)*--('Resource Sheet'!$A:$A=B1)))-1,0,1,COUNTA('Resource Sheet'!1:1)+COUNTBLANK('Resource Sheet'!1:1))
Breaking it down:
Resource Sheet'!$A$1 : This tells us which sheet to look in
--
,SUMPRODUCT(MAX(ROW('Resource Sheet'!$A:$A)*--('Resource Sheet'!$A:$A=B1)))-1 : This tells us which row to look at, but I'll explain it later.
--
,0,1 : This tells us that we start in Column A ("Move 0 columns right"), and only want 1 row high.
--
COUNTA('Resource Sheet'!1:1)+COUNTBLANK('Resource Sheet'!1:1) : This makes sure we look at the whole row.
--
Now, to explain the last bit, with the SUMPRODUCT. It ends with a -1, because from $A$1 we move 0 rows down to finish on Row 1, or 2 rows down to finish on Row 3, etc. The SUMPRODUCT itself is just to force it to evaluate as an Array Formula - and the formula itself is MAX(ROW('Resource Sheet'!$A:$A)*--('Resource Sheet'!$A:$A=B1))
MAX : We want the largest value, because the rows we don't want will retun 0
--
ROW('Resource Sheet'!$A:$A) : This will be an Array for all of the row numbers, {1,2,3,4...}
--
--('Resource Sheet'!$A:$A=B1) : The inside bit will create an array of TRUE and FALSE for each row if the value in Column A matches our Project Name. The -- converts TRUE to 1 and FALSE to 0 Using your examples above, P1 would give {0,1,0,0,0,..} and P2 would be {0,0,1,0,0,..}.
Multiply the 2 arrays together, and for P1 you get MAX({0,2,0,0,0,..}) and for P1 you get MAX({0,0,3,0,0,..})

With your dropdown containing the choices (
P1, P2, etc) in A2 you can use this in an adjacent cell.
=CHOOSE(MID(A2, 2, 2), "primary", "secondary", "secondary", "secondary", "secondary")

Related

VBA : distinct values in a column and their associated distinct values

I'm trying to do something similar in vba which I have an idea of only in python for loops. Can someone teach me how to do this in vba, either in function or module macro please :
For each distinct values in column A4:A30, there should be no more than 9 distinct values in column C4:C30. If true, return 'OK' in cell A1. if false, return 'Error' in cell A1'
e.g As in the picture, Sam should not have more than 9 distinct fruits. Same goes to Mary
Update :
I have tried the filterxml method and unfortunately didn't seem work for me : [1] https://i.stack.imgur.com/cbmTs.png
Solution for excel with filter/unique formulas
Easiest way to achieve it in Excel365 is: add extra column which counts unique values (Fruits) for each Key (Names) and find maximum value in this column
Start with formula that find each non-blank which fits the key.
=FILTER($C$4:$C$30,($A$4:$A$30=A4)*($C$4:$C$30<>""))
Then delete duplicates:
=UNIQUE(FILTER($C$4:$C$30,($A$4:$A$30=A4)*($C$4:$C$30<>"")))
Then check how many cells we have in filtered data without blanks and duplicates:
=COUNTA(UNIQUE(FILTER($C$4:$C$30,($A$4:$A$30=A4)*($C$4:$C$30<>""))))
Then expand our new-column (column B in my case) formula to each row in our Keys.
And finally add formula to A1 which checks maximum counter:
=IF(MAX($B$4:$B$30)<10,"OK","Error - to many velues")
*There is a little typo, it should be "Error - to many values" =)
Below how the worksheet looks in my testfile
Solution for older versions of excel
I've checked if i am able to make it works without these formulas and it is possible:
We need to start with counting if there is for key-value above current row
=COUNTIFS($A$4:A4,A4,$C$4:C4,C4)
In case we have duplicates above, they should be already counted so we skip them:
=IF(COUNTIFS($A$4:A4,A4,$C$4:C4,C4)>1,"",1)
Now we have colum with "1" or blanks. In that case we need to count each non-empty cell above which correct key (name) and add 1 so instead "" and "1" we will have "" or 1, 2, 3, 4, ...
=IF(COUNTIFS($A$4:A4,A4,$C$4:C4,C4)>1,"",COUNTIFS($A$3:A3,A4,$F$3:F3,">0")+1)
Edit
I have added one extra IF to skip keys if value is blank:
=IF(C4="","",IF(COUNTIFS($A$4:A4,A4,$C$4:C4,C4)>1,"",COUNTIFS($A$3:A3,A4,$B$3:B3,">0")+1))
Cells Formula in A1 is the same
=IF(MAX($B$4:$B$30)<10,"OK","Error - to many values")
Quick Note:
Some formulas have range which starts on 3rd row instead of 4th; Its intended because we are counting cells above and at first row of data we need to have choose something above. This code assumes that you don't have numbers (on column B) or names (on column A) in row 3;
Below I am attaching screen with example; This screen have additional columns (D-F) which isn't required, its only do display how final formula was created.

Text and numerical values in pivot tables in Excel

I have a table in Excel that looks like this:
Name QuestionID Answer
A 1 Shoes
B 1 Coats
A 2 1983
B 2 1978
I want to create a table that Looks like this:
Name 1 2
A Shoes 1983
B Coats 1978
I've tried creating a pivot table, where "Name" is the row, "QuestionID" is a column, and "Answer" is the value. But values need to be aggregated, so I wind up with something numerical, instead of just the text or numbers in "Answers." I'm not clear on how to just display just the values, and not some processed version of the values.
Any help would be really appreciated!
This is why I never use Pivot Tables...they never seem to do what I need them to do.
Assuming that you can create a unique distinct list for the Header row & Name column manually, or auto-magically (see unique distinct list)
You could just use the following formula inside the table to do a lookup:
=INDEX($C$2:$C$5,SUMPRODUCT(($A$2:$A$5=$E2)*($B$2:$B$5=F$1)*ROW($C$2:$C$5))-ROW($C$1))
Where
Source Table is in A1:C5
Result Table - Name Column is E1:E3
Headers (for Questions) are in cells F1:G1
If you break it up, it's just an INDEX table lookup
=INDEX($C$2:$C$5,SOME_ROW)
Where SOME_ROW is determined by
SUMPRODUCT(($A$2:$A$5=$E2)*($B$2:$B$5=F$1)*ROW($C$2:$C$5))
Breaking the SUMPRODUCT down, we're just creating set of lists, of 1's & 0's (or True/False) for the search criteria.
The first column to match the Name is determined by ($A$2:$A$5=$E2)
The second column to match the Question is determined by ($B$2:$B$5=F$1)
Multiplying the two lists together, when Name = A & Question =1 you get
(A,B,A,B) * (1,1,2,2)
(1,0,1,0) * (1,1,0,0) = (1,0,0,0)
The third list (that we are going to multiply the above result with) from the SUMPRODUCT, is the actual Row; it's determined by ROW($C$2:$C$5), so we get:
(1,0,0,0) * (2,3,4,5) = (2,0,0,0)
Now we SUM the PRODUCTS of the result:
2 + 0 + 0 + 0 = 2
So the actual result is in Row 2; Yes!
...but we have a problem; in the INDEX function, the the Array starts on Row 2, so if we specify the 2 for the row number, we'll actually get the value from row 3 because of the starting row offset.
This is where -ROW($C$1) comes in; to fix the row offset. It should point to a cell in the header row.
It's much easier then it looks...

Search a date in multiple date ranges and return the corresponding value

In sheet 1, I have a list of dates in Column A in chronological order. There are values corresponding to this list in Column B. There is another list of dates in sheet 2 and I want to add values from sheet 1 to these dates.
Sheet 1.
**Column A Column B
DATE Amount**
1. 10/01/2015 25,60,000
2. 10/02/2015 26,80,000
3. 01/03/2015 21,55,000
4. 30/03/2015 24,60,500
5. 30/04/2015 28,20,000
6. 30/06/2015 19,00,000
Sheet 2.
Column A Column B
1. 21/02/2015 21,55,000
2. 15/01/2015
3. 20/05/2015
4. 25/04/2015
For example: I need to look up 21/02/2015 in sheet 1 and column A and return the value corresponding to the next available date. So for 21/02/2015 I need the value corresponding to the next date available which is 01/03/2015 and the value is 21,55,000. If its 15/01/2015 I need the value of 10/02/2015 i.e. 26,80,000
What formula could I use for this?
You could use VLOOKUP, but it has some issues. So it is better to use INDEX and MATCH combination. In your case try this
=INDEX('Sheet 1'!$B:$B,MATCH(A1,'Sheet 1'!$A:$A,-1))
Sorry, my previous answer works only for descending order. Try this instead
=INDEX('Sheet 1'!$B:$B,MATCH(TRUE,('Sheet 1'!$A:$A-A1)=MIN(IF('Sheet 1'!$A:$A-A1>=0,'Sheet 1'!$A:$A-A1)),0))
Explanation: I hope that INDEX and MATCH are well explained in Office Support.
About the condition:
('Sheet 1'!$A:$A-A1)=MIN(IF('Sheet 1'!$A:$A-A1>=0,'Sheet 1'!$A:$A-A1))
What it means?
'Sheet 1'!$A:$A-A1
results in a difference between the value in the cell A1 and the cell in A column in Sheet 1.
MIN(IF('Sheet 1'!$A:$A-A1>=0,'Sheet 1'!$A:$A-A1))
says that if the difference is non-negative ('Sheet 1'!$A:$A-A1>=0), find the minimum of such numbers (MIN function).
And if these numbers are equal (MATCH function), then pick the corresponding number in column B (INDEX('Sheet 1'!$B:$B,...)).
Apology: In my previous answers I swapped the columns of your example. I hope it is now correct.
You can use vlookup with True rather than the widely used form with False
As ExcelEfendisi said you can use vlookup with range lookup enabled. A simple way to get the value at the next date rather than the prior one would be to push all the amount values down one row, but to avoid that it might be better to repeat the index values - like this
1 10/01/15 25,60,000 1
2 10/02/15 26,80,000 2
3 01/03/15 21,55,000 3
4 30/03/15 24,60,500 4
5 30/04/15 28,20,000 5
6 30/06/15 19,00,000 6
Then you can use two vlookups, the first one to get the index of the row with the date prior to the date you are interested in and a second one to extract the balance value for the subsequent date - not very elegant but it would work
Try this formula (enter in Sheet2 cell B2 then copy till the last record)
=INDEX(Sheet1!$B:$B,1+MATCH($A2,Sheet1!$A:$A,1),1)
As data is sorted in ascending order use MATCH with match type 1 (less than) to obtain the row above the high next item, then add 1 and the result is the high next row, use this row to get the corresponding record from the column B with formula INDEX

Need to pull the cell's info or mark it as "x" if there's specific text containing in a different column

I've following data set:
For Account 1, if column 6 contains NBA, column 4 (NBA) should be checked as "x or pull "NBA or NBA1" - whatever version of NBA it is;
If Account 2 has MNM, put an "x" on the column MNM.... similarly for each account down the column.. I tried putting index, small functions but keep getting blanks. Any thoughts?
Column1 Column 2 ABC NBA MNM Column 6
Account1 CNN x NBA
Account2 N-H x MNM5
Account3 ABC is good ABC
Account4 H-I ABC2
Account5 TI MNM3
Account6 N-T NM
Account7 BE NBA1
Assuming the different 'versions' in column 6 mean nothing, and count as a 'regular' result (ie: NBA is the same for these purposes as NBA1), see the following:
Put this formula in Column3, row 2 (ABC Account1) and drag down/right:
=IF(ISERROR(SEARCH($F2,C$1)),"","x")
This says: if you find the text of the current header column in row 1, within the item in column F for this particular row, then put "x". If that search produces an error, show "".
Im trying to ascertain what you actual are trying to acheive, doe the numbers at the end of your data in column six mean something or does MNM5 still mean mnm.
If so I went a slightly different route to Grade Eh Bacon.
I added another column called truth of which I used an if statement to look for the "ABC","NBA","MNM"
=IF(ISNUMBER(SEARCH("NBA",F2)),"N",(IF(ISNUMBER(SEARCH("MNM",F2)),"M",IF(ISNUMBER(SEARCH("ABC",F2)),"A"," "))))
The F2 Reference is, Column 6 first row of data.
I then went back into your "ABC","NBA","MNM" columns and added:
=IF($G2="A","X"," ")
"A" is the true value for ABC.
"N" is the true value for NBA.
"M" is the true value for MNM.
If the truth column under each prospective column has this Variable then it will mark the cell "X"

Multiple Lines in Excel, need a Formula to calculate diferences

I have an inventory spread sheet.
There are 5700 rows all up to column "I".
These rows have part numbers in column "A".
Column "I" contains the cost of that part based on a quantity of parts listed elsewhere in the table.
With me so far?
Lets say Row "1" contains a part number 9FH-01010 and column "I" gives a dollar value based on other criteria, (number of parts BEFORE Inventory).
Row "2" would be the same with the exception that row "2" is based on the dollar value AFTER inventory.
Is there a formula that could identify like cells in column "A" and then calculate the difference in dollar value of "I" and then place that answer in column "J"?
Here's the kicker, there are some part numbers that have only 1 entry.
Example:
"A1" = 9FH01010 "I1" = 5000.00 "J1" = -2500.00
"A2" = 9FH01010 "I2" = 2500.00
"A3" = 9FH20202 "I3" = 4300.00
"A4" = 9FH30303 "I4" = 2000.00
"A5" = 9FH30303 "I5" = 4000.00 "J5" = 2000.00
Any help on this is appreciated.
What I'm getting is that (1) in each row, there is a key in $A; there will either be exactly 1 following row with the same key, or zero rows.
Have a look at the 'IF' function. This is probably the easiest way to code the function:
IF $a is equal to $a, then compute $I - $I.
Something like this:
=IF($A2 = $A1, $I2 - $I1, ' ')
In column A of your database you have ID's for the products, but they can appear twice. One for each circumstance (AFTER and BEFORE). I advice you first to add a column to determine which one is which.
The following formula works only if each product appears twice ("after" and "before")
in a NEW column, next to I5 -for example in J5- TYPE:
=IF(VLOOKUP(A5,A$1:I4,9,)>0,I5-VLOOKUP(A5,A$1:I4,9,),"")
then copy J5 up and down through the whole column.
If you want to avoid errors (#N/A), you can do a larger formula with IFERROR function.
Based on the sample data, assuming your data is in range A1:I5700
You can enter the following formula in cell J1 and drag it down to apply across:
=IFERROR(INDEX(I2:$I$5700,MATCH(A1,A2:$A$5700,0))-I1,"")

Resources