If statement across MS Excel sheets - arrays

BACKGROUND (simplified version): I’m sorting data sheets of different items for our lab. I have one sheet that lists the code number of the item (column A) with the actual name of the item (column B), for example
code | item
001 | Banana
002 | Cabbage
003 | Carrot
004 | Peach
and another sheet (labelled stocks) that lists only the code number (column A) and the amount of stock we have for that item (column B), for example
code | stock
001 | 5
002 | 6
003 | 2
004 | 7
PROBLEM: Now, these items can be sorted into 2 categories, “fruits” and “vegetables”. I can easily sort the items in the item sheet manually because I can see the actual item name, but for the stock sheet, only the code number is listed which means I would have to keep looking at the other sheet to see which item is associated with a particular code.
I already have separate item sheets for fruits and vegetables, so I have three sheets total now: fruits, vegetables, and stocks.
I was thinking of an IF statement to be placed in the stocks sheet that would indicate if the item is a fruit or a vegetable. Something like: if the code in A1 of the “stocks” sheet is present in column A of the “fruit” sheet, then print the word “fruit” in C1 of the “stocks” sheet. If not, print the word “vegetable” in C1.
My ultimate goal (what the lab is looking for) is that I would have separate item sheets for fruits and vegetables (already done) and also separate stock sheets for fruits and vegetables (to be done).
Would appreciate any help. Many thanks

VLOOKUP works well for this.
Make a third column in the stock sheet with the following formula in Cell C2 and copy it down to the cells below:
=VLOOKUP(A2,Fruits!$A$2:$B$200,2,FALSE)
This takes the value in cell A2, find the same id in the table in sheet 1 and returns the value that is given in the 2nd column of the Fruit Worksheet.
If you want the tables fruits and vegetables separate. You can use IFNA to find the id in the second worksheet as followed:
=IFNA(VLOOKUP(A2,Fruits!$A$2:$B$200,2,FALSE),VLOOKUP(A2,Vegetables!$A$2:$B$200,2,FALSE))

if the code in A1 of the “stocks” sheet is present in column A of the “fruit” sheet, then print the word “fruit” in C1 of the “stocks” sheet. If not, print the word “vegetable” in C1.
This is a simple test across the worksheet. This formula searches range A:A in the "Fruit" sheet, and if there is a match (returned as a number) it gives the first value "Fruit", otherwise it will return "Vegetable".
=IF(ISNUMBER(MATCH(A1,Fruit!A:A,0)),"Fruit","Vegetable")
However be careful with this, because if the code is wrong of not found, it will return "vegetable" by default. To get around this, use a nested IF statement which checks both sheets like so, and returns "Not found" if the code is wrong.
=IF(ISNUMBER(MATCH(A1,Fruit!A:A,0)),"Fruit",IF(ISNUMBER(MATCH(A1,Vegetable!A:A,0)),"Vegetable","Not found"))
EDIT: I've swapped the SEARCH for the more powerful MATCH since this will negate display options and still find the correct sheet.

Related

How do I Add Values from Worksheet 1 to Another Worksheet in the same Excel File according to an unique ID Nr?

I would like to retrieve average daily milk from sheet 1 to sheet 2. Each row has a unique identification number. The average daily milk data exported to sheet 2 is linked to the unique ID Nr. In sheet 1 there are many more unique ID.Nr ( Rows) present than in sheet 2. How do I transfer the data from sheet 1 to sheet 2 in exact correlation to identification nr, without sorting the rows?
I have tried vlookup function but result is always NA. I belive it should be an array function, as it has too look for ID Nr. and cannot use position of cell.
Table Sheet1 is on a different Worksheet than Sheet 2 but on same File.
Vlookup should work just fine. The only thing is that you have to make sure that the Id Nr. in both sheets are the same type (In this case both should be strings since they have "_" in the text). This is how you should use the vlookup:
=VLOOKUP(A2,Sheet1!A:B,2,FALSE)
And then copy the formula all the way down in column B of sheet2
The solution is to use the index match function. The advantage in comparison to an lookup function is that it handles great amount of data better and also the reference number does not need to be on the far left column.

Index Match in Array in Google Sheets

I have a spreadsheet 'Database' where there are two columns, 'Countries' (A) and 'Companies'(B). Countries are numbered from 1-10. Companies are labelled with letters.
The spreadsheet tracks the companies that operate in each country. As such, a row that has Country 1 in column A and Company A in column B means that Company A operates in country 1. This is a screenshot from this spreadsheet:
I then have a second spreadsheet called 'Countries by Company' where I would like to list the countries in which a company operates. I would like them to be listed in different rows starting form cell B3 of the 'Countries by Company' sheet. The company name for which the countries should be listed is in cell B1 of the same sheet. I gather this has to be done with an ARRAY version of an INDEX MATCH function but I can't seem to get it to work.
I have put the following formula in cell B3 of the 'Countries by Company' sheet: =INDEX(Database!$A$1:$B$76, MATCH($B$1, Database!$B$1:$B$76, 0), COLUMN(Database!A:A)). However, as expected, it will only return the first country in which the company in cell B1 operates and no other country. Again, I would need all countries in which that company operates listed in different rows from cell B3.
I have set up a sample sheet in here. I thought it might help in case I wasn't too clear.
Thanks for your help!
Using FILTER allows restricting the values on the Database sheet to only those that match a given criteria:
=FILTER(Database!$A:$A, Database!$B:$B=$B$1)
yields every country index for the given company index on the Database sheet. If only the country index values (e.g. 1, 3, 6) are desired, rather than converting those index values to a name or other property that is held in a hypothetical "Country DB" sheet, the above FILTER is sufficient.
If additional information is desired, then VLOOKUP can be used to return the corresponding values for multiple inputs (i.e. an array input):
B3: =ARRAYFORMULA(VLOOKUP(
FILTER('Database'!$A:$A, 'Database'!$B:$B=$B$1), // Searched values
'Country DB'!$A:$B, // Database sheet having more country information (like name, gdp, ...)
2, // Column of the desired information
FALSE))
will yield the 2nd column from each corresponding country index row in the "Country DB" sheet:
Try using Filter
=filter(Database!A2:B,Database!A2:A=B1)

How to return the column heading for multiple matches, from multiple criteria, in Excel?

I'm working with 40+ price lists for different groups of customers. By entering the item number and price, I'd like to show the names of all the price lists that match that item/price. I've set up the items and pricing lists like the table below. Some items have the same prices in multiple price lists, and some price lists do not list all items.
Item ListPrice Red Blue Green Grey
Hosaka2 $200 $180 $188 $171
TrodesH $460 $410 $380 $400 $380
TrodesL $810 $680 $680 $720
Shuri $80 $72 $72 $70
I'd like to enter TrodesH and $380 into cells and have the formula return Blue & Grey. Or enter Hosaka2 and $200 and have the formula return ListPrice.
I've used this Index array formula in the past to return multiple matches to a single entry. To, for example, list all the account numbers whose address matches the zip code I enter. It's very handy. But here I'm looking for the combination of item and price. Any ideas how I might move forward?
Edit: For better legibility in the Excel file, I've placed the above data in a worksheet named "Matrix" and the lookup formulas in a worksheet named "Check."
Let's assume that A1:F5 contains the data, and H2 contains the item of interest, such as TrodesH, and I2 contains the price of interest, such as $380, try...
J2:
=COUNTIF(INDEX($B$2:$F$5,MATCH($H2,$A$2:$A$5,0),0),$I2)
K2, confirmed with CONTROL+SHIFT+ENTER, and copied across:
=IF(COLUMNS($K2:K2)<=$J2,INDEX($B$1:$F$1,SMALL(IF(INDEX($B$2:$F$5,MATCH($H2,$A$2:$A$5,0),0)=$I2,COLUMN($B$1:$F$1)-COLUMN($B$1)+1),COLUMNS($K2:K2))),"")
Hope this helps!
Taking the diagram below as a model (adjust it to your own data layout and places), you can enter this formula at J1 and copy/paste into the colored area:
J1:
=IFERROR(INDEX($1:$1,AGGREGATE(15,6,COLUMN($B$2:$F$5)
/($A$2:$A$5=$H1)/($B$2:$F$5=$I1),COLUMN(A:A))),"")

excel assemble two sheets exported from database

I have two sheet of excel exported from a database with hundreds of rows.
In first sheet I've these columns
name age gender
id1 23 m
id2 45 f
In second sheet these columns
id1 john smith
id2 jean ford
I'm looking for a macro or somethig else to automatically replace the idx in first sheet with the corresponding values from second sheet.
The final result should be a sheet like:
name age gender
john smith 23 m
jean ford 45 f
You don't need anything as complicated as a macro-- VLOOKUP will suffice:
Searches for a value in the first column of a table array and returns
a value in the same row from another column in the table array.
The V in VLOOKUP stands for vertical. Use VLOOKUP instead of HLOOKUP
when your comparison values are located in a column to the left of the
data that you want to find.
For example, if your id-sheet mapping was on Sheet2, then the formula
=VLOOKUP(A2,Sheet2.$A$1:$B$2, 2)
would look for the value found in this sheet's A2 cell in the leftmost column of the data table located in Sheet2.$A$1:$B$2, and then return the value from the 2nd column of that table. Copy that downwards, and get something like

How to merge two Excel sheets

I have an Excel document with 10000 rows of data in two sheets, the thing is one of these sheets have the product costs, and the other has category and other information. These two are imported automatically from the sql server so I don't want to move it to Access but still I want to link the product codes so that when I merge the product tables as product name and cost on the same table, I can be sure that I'm getting the right information.
For example:
Code | name | category
------------------------------
1 | mouse | OEM
4 | keyboard | OEM
2 | monitor | screen
Code | cost |
------------------------------
1 | 123 |
4 | 1234 |
2 | 1232 |
7 | 587 |
Let's say my two sheets have tables like these, as you can see the next one has one that doesn't exist on the other- I put it there because in reality one has a few more, preventing a perfect match. Therefore I couldn't just sort both tables to A-Z and get the costs that way- as I said there are more than 10000 products in that database and I wouldn't want to risk a slight shift of costs -with those extra entries on the other table- that would ruin the whole table.
So what would be a good solution to get the entry from another sheet and inserting it to the right row when merging? Linking two tables with field name??... checking field and trying to match it with the other sheet??... Anything at all.
Note: When I use Access I would make relationships and when I would run a query it would match them automatically... I was wondering if there's a way to do that in excel too.
Why not use a vlookup? If there is a match, it will list the cost. Assuming the top is sheet1 and the other sheet2 and they both start on cell A1. You just need this in cell D2.
=VLOOKUP(A2,Sheet2!A:B,2,0)
You can then drag it down. Easiest way to fill all your 10000 rows is to hover over the bottom left corner of the cell with your cursor. It will turn from a white plus sign into a thin black one. Then simply double click.
Just use VLOOKUP - you can add a row to your first sheet, and find the cost based on code in the other sheet.

Resources