find matching values in 2 different columns in 2 different excel file - excel

Hi new to vba in excel 2007
Here is the scenario I want to write a macro where a value in column A from abc.xls is in column c from .xyz.xls. If someone can help me out with this logic and can easily finish the rest. Thank you for your time.

Welcome to SO. Going on what Tim said in his comment vlookup() is an easy way to find a value in another sheet. In your case the function would look something like this:
vlookup([abc.xls]Sheet1!A1, [xyz.xls]Sheet1!C:C, 1, False)
The first part is the value to look up, the second part is the table to look for the value in (in our case only the one row), the third part is which column of the table to return the value from, and the third part tells it to find an exact match. So this function will look for the value in A1 of abc.xls in column C of xyz.xls and return that value if it finds it.
If you instead want the row where the value was found use the match function.
match([abc.xls]Sheet1!A1, [xyz.xls]Sheet1!C:C, 0)
This will do the same thing as vlookup but return the row where it found the match instead.
Note that you don't have to type these formulas in directly. If you navigate to a new workbook and select the cell it should generate the reference just as if you had selected one from the current sheet.

Related

Excel - Retrieving a cell based off of multiple criteria

I am attempting to return the value of a cell from within a different sheet based upon two cell values in the second sheet.
As an example the Main and Second sheet have three columns each (Item, Code, Number), the difference being that the second sheet has only one row and two drop down lists that are populated with the Item(s) and Code(s) (from the Main sheet - i.e like a database table). From within the Second sheet I would like the Number to be returned from the main sheet, once the Item and Code had been selected from the drop down lists on the Second sheet.
Unfortunately I'm unsure as how to do this based off of multiple criteria. As far as I'm aware VLOOKUP only used one criteria over a range. I've attempted the following below which is accepted by Excel as a valid formula, however it doesn't return the correct value.
{=SUMPRODUCT((Main!A:A='Second'!A7) * (Main!C:C='Second'!E2) * (Main!G:G))}
I've interpreted the above as the first two sets of brackets being the range and condition to match and the last set being the range of values to return from. Is this a correct assumption and is this the correct use of that function?
Are there any other functions I could use?
Thank you, I have very little experience with Excel!
Have you taken a look at the magic INDEX combined with MATCH, and perhaps MATCH again?
INDEX returns the contents of a cell from a table provided a row and a column. Usage:
=INDEX([table to look in]; [COLUMN index]; [ROW index])
MATCH returns the indexes that match a criteria in the table. Usage:
=MATCH([Criteria];[table to look in])
And you can nest one inside the other the following way:
=INDEX([table to look for in];MATCH([Criteria];[Table to look for the particular Row]);[COLUMN, or a second MATCH to find the right column])
Or even combine a couple of MATCH as in
=INDEX([table to look for in];AND(MATCH(1);MATCH(2)); [Column to look for, or MATCH to look for in column])
Which I think is what you want.
If you need a SUM of something (as you used a SUM function before), just nest it inside the proper SUM function

Excel - Conditional row from an array to COUNTIF from VLOOKUP

I don't know if I'm going about this the wrong way but it seems like it should be simple. Column A has a list of Names. Along each row is several "W"'s. Another separate field has a drop down representing Column A names. I want to count the number of "W"'s in a row corresponding to what name I select. I've tried using VLOOKUP and COUNTIF but I can't figure out how to select the entire array and then single out the one row that matches my selected name. I can get it working with a bunch of IF statements but thats far too time consuming as I'm manually matching the name to the row (and it isn't future proof).
There are a few ways to first 'narrow in' on the row you're looking for, after which point you can use a simple COUNTIFS to check the number of W's in that row.
One method would be to simply use INDIRECT, and create the row reference on the fly, like so [assumes your search cell is C1]:
=COUNTIFS(INDIRECT(MATCH(C1,A:A,0)&":"&MATCH(C1,A:A,0)),"W")
This first uses MATCH to find the appropriate row, and then builds a reference to that row [like "24:24"], which becomes the row that INDIRECT passes to COUNTIFS, which counts that row for W's.
For only one use of INDIRECT, the high computing costs of INDIRECT should not be an issue.
Another method would be to point out the full possible box that data could be contained in [let's assume that at most only column H would be used], and then use INDEX to give us the appropriate row number, like so:
=COUNTIFS(INDEX(A:H,MATCH(C1,A:A,0)),0,"W")
This again uses MATCH to find the row which contains the value found in C1 within column A. Then it takes the full possible box from INDEX, and returns all columns from the particular row [note that telling index to return 0 for the column # actually returns all columns instead].
Other methods would be possible [for example OFFSET], but I believe these two show the principle fairly well.
You could use the "Helper" Column method:
In the helper column:
=COUNTIF(B2:H2,"W")
Then use SUMIF() in the totals column:
=SUMIF($A$2:$A$9,K2,$I$2:$I$9)

Clarification of parameters in an INDEX MATCH formula

I am working through a spreadsheet created by someone else and in one Worksheet Column A has a value obtained with the formula below;
=INDEX(Sites!A$2:AC$10000,MATCH(F2,Sites!P$2:P$10000,FALSE),3)
I am confused by this formula and wondered if someone could clarify it for me please. I have never used an INDEX MATCH formula before and when I google for it, there are no examples that use a FALSE or TRUE before the end value, in this case "3". Also if the last value is "3" or the third column in the lookup range - how can this be when the P$2:P$10000 only has one column, Column P?
Here is a screen image of the worksheet
The FALSE works but is wrong.
With the MATCH function the 3rd parameter is optional. If not specified it defaults to the value of 1, which tells the MATCH function that the data to search is sorted and to therefore use a Binary Search algorithm to do the matching... which is really fast.
A value of zero, tells the MATCH function to instead do a linear search from top to bottom until the value is found, or not found at the end. This is called an Exact-Match search. The value 0 should be used here. It just so happens that FALSE evaluates to zero, but there is no reason to use it here, and it is just misleading. It should be simply 0.
...
Regarding the 3 at the very end. This instructs the INDEX function to return the corresponding value in the THIRD column of: Sites!A$2:AC$10000.
Index Match is a pretty powerful combination. It will return a value from an Index (which you set via a range), then uses a Match() to get the row.
In your example, the formula will return some value in the range A2:AC10000. [I think though this needs to be edited to just A2:A10000?]
Great, but which cell in that range? We need to know a row. Think of it like getting a cell in a cross section. The Index part is the column, and now you need a way to say what ROW to use. This is where Match() comes in.
Match first uses some value you want to find. In your example, it's looking at the building name ("Kilibarachan Primary"). This name exists in a column somewhere, Column P. It will find that factory name in column P, and return the row number. This row number is then fed to the Index. Now you have the column (A) and the intersection row.
Edit: Excel Hero beat me to this, but I figured I'd leave it anyways.

Excel: Vlookup in named range and return a value outside of range

I need to find a way to search for a value in a named range and to return the column next to that range. The reason is simple: I do use list validation with a named range in column A. In this list there are complete name of product types (ex. Relay, Contactor, Enclosure) and I need to return a short description from column B (ex. Rly, Cont, Encl) so that when the user searches the list it's easy to find what he is looking for. I know I could extend my range to $A:$B but if I do then all the values of $B will be included in the list...
This shows a way to figure out what I need: =VLOOKUP(A1;"RANGE+1C",2,0)
I tried so many ways with offset and index but couldn't find a way to do so... I even thought it would be possible of doing it with relative reference (ex. =VLOOKUP(A1,Description;1"RC[1]",0) or something like that...
Also looked in Google to find some info but it seems to be something very unfamiliar...
I need to do this in a formula and not in VBA.
Here is the link to an example file:
http://www.filedropper.com/descriptionbuilder
Any hint?
Thank you!
What you probably want instead of VLOOKUP is the MATCH & INDEX combo.
MATCH is a function which tells you the row number where a duplicate value is found from your selected list - it's like half of VLOOKUP. INDEX is a function which pulls a value out of a list, based on its position # that you give it - it's like the other half of VLOOKUP.
I see your one Range in column A with your ID data is Description - I'll assume that column B has no 'named' range that aligns with yours. I'll assume that column B has other data, and that column A starts somewhere after row one [specifically, I'll assume it starts at row 5]. Together the formula would look like this:
=INDEX(B:B,ROW(A5)-1+MATCH(A1,Description,0))
This says: find the row # within Description that matches the text in A1. Then add the row # of A5 - 1 (this then alignts the top of Description with the top of column B). Then pull that value from column B.
Alternative Method using VLOOKUP and OFFSET
Another method would be to first define how many rows are in Description, and then using that information and the OFFSET function, create a new area which essentially represents Description but going across column A & B instead of just column A. This would look as follows (again, assuming Description starts at A5):
=VLOOKUP(A1,OFFSET(A5,0,0,ROWS(Description),2),2,0)
This says: count how many rows are in Description. Build an array with OFFSET which starts at A5, with as many rows as there are in Description, and 2 columns. (So with 3 items in Description, this would be A5:B7). Then use that new Range in VLOOKUP, and try to find A1 the Description area in column A, and return the result from that row in column B.
You can use Offset to expand a named range by one column and then feed it into Vlookup Something like:
=VLOOKUP(C1,OFFSET(RANGE,0,0,,1+COLUMNS(RANGE)),1+COLUMNS(RANGE))
To test is, I created a named range (called "RANGE") in Column A, I placed values in Column B adjacent to the named range, put a lookup value in C1 and the entered the above formula. It succeeds in retrieving the desired value.
On edit: I think that Grade'Eh'Bacon's answer is excellent and is probably the best approach to this specific problem, but it strikes me as potentially useful to know how to expand the reference of a named range without changing the name itself, so I am keeping this answer.

Get current row in from sumif statement

I'm using Excel 2010 and have a datasheet with multiple tabs, this is the current SUMIF function that I am using
=IF(SUMIF('Master Data'!$J$2:$J$200,'Resource View (2)'!B22,'Master Data'!$W$2:$W$200)>0,Current_row_different column, "")
Basically, I find some rows in the other sheet that have a value of 1 I then want to use these rows that have a value of 1 but use a different column within that row to populate the true condition.
So say for instance row 4 contains a 1 at column A I then want to stay on row 4 but get the value of column B for the true condition.
Is this possible?
EDIT:
I have now managed to get the function working however its a bit of an Excel hack, because I have had to move the columns around in the master data and its a bit messy now anyway here's what I've got
=IF(SUMIF('Master Data'!$C$2:$C$200,'Resource View (2)'!B22,'Master Data'!$W$2:$W$200)>0,VLOOKUP(B22,'Master Data'!$C$2:$F$90,3,FALSE),"")
Now I know this is because VLOOKUP searches through the first column specified and it doesn't seem to work at all if i try to put the range in backwards i.e. $F$90:$C$2 instead of $C$2:$F$90. Is there any way to manipulate VLOOKUP to work like this?
Yes, actually there is a way - a brother of VLOOKUP - it's INDEX(MATCH()). It is a very useful tool, as you can look at any column and return any other, not only looking at the first one to return the ones to the right. Also, INDEX(MATCH()) can be used in forming array formulas, if you need that, while VLOOKUP cannot.
Basically, this part of your formula:
VLOOKUP(B22,'Master Data'!$C$2:$F$90,3,FALSE)
would be changed with this:
INDEX('Master Data'!$E$2:$E$90,MATCH(B22,'Master Data'!$C$2:$C$90,FALSE))
So, after all, the equivalent for
=IF(SUMIF('Master Data'!$C$2:$C$200,'Resource View (2)'!B22,'Master Data'!$W$2:$W$200)>0,VLOOKUP(B22,'Master Data'!$C$2:$F$90,3,FALSE),"")
would be
=IF(SUMIF('Master Data'!$C$2:$C$200,'Resource View (2)'!B22,'Master Data'!$W$2:$W$200)>0,INDEX('Master Data'!$E$2:$E$90,MATCH(B22,'Master Data'!$C$2:$C$90,FALSE)),"")

Resources