Copy corresponding values from reference to a new list in Excel 2007 - vba

I hope somebody can help me; I've been trying to figure out how to do this all day long.
I have one sheet with a list with corresponding values, e.g. Columns A, B, C make a group of values that have to be together.
In another sheet, I have some values from Column A, but not all of them, and want to automatically add the corresponding columns B and C to it.
So I thought about making a macro that copies the first value from Column A in the incomplete sheet, searches for it in the reference sheet, copies the corresponding values from B and C, pastes them next to A in the incomplete sheet, then copies the next value from A and searches for it in the reference table, and so on...
I tried but couldn*t make it work - any ideas? I've wasted way too much time on that problem already! Would do it by hand but have like 40,000 rows of data.
If you know how to do it, help would be MUCH appreciated.

Based on these assumptions:
Complete data is in Sheet1, columns A:C starting at row 2 (Headings in row 1)
Incomplete data in sheet2, column A starting at row 2 (Headings in row 1)
put this formula in sheet2 cell B2
=VLOOKUP($A2,Sheet1!$A:$C,2,0)
put this formula in sheet2 cell C2
=VLOOKUP($A2,Sheet1!$A:$C,3,0)
copy these formula down for as many rows as you have data
you can leave the formulas in place, or copy paste values to make permanent

Related

How do I insert a row in excel and include the inserted row in a formula?

Example:
I have a cell "A1" that sums up some values in a row "IF"-something: =SUM.IF($C$5:$C$10;"Blue";$D$5:$D$10)
meaning: If columns C5 to C10 contains the word "Blue" in any row for example. "C7" and "C9", then the sum of "D7" and "D9" will be shown in "A1".
My problem:
If I insert a new row "5", then I want the formula to contain this row as well:
=SUM.IF($C$5:$C$11;"Blue";$D$5:$D$11)
But what happens is that the formula is now:
=SUM.IF($C$6:$C$11;"Blue";$D$6:$D$11)
So the formula contains only the original 5 rows "5-10" and now named "6-11".
How do I make excel expand the formula to include the new row aswell so it sums up from "5-11" =SUM.IF($C$5:$C$11;"Blue";$D$5:$D$11)?
(I know I can make this work with a table, but this is a thought example and not my real situation. Actually I insert 5 rows at a time, and some cells are merged, so I can't convert the range of data into a table)
Any help is greatly appreciated.
This can't be done. Set the row range reference in the formula to one row above you actual list, then everything should work.
For example, leave row 5 empty. Hide the row if you like. In cell A1, use the formula =SUMIF($C$5:$C$10,"Blue",$D$5:$D$10) (starting at row 5). Start entering values in row 6.
Now you can right-click Row 6 and Insert a new row, and the formula will continue to work as required.

Compare Excel sheets values to update a third value

Example file So I have two sheets that each have lists of part numbers, plant where they come from and two columns on costs. What I need to do is scan them and if Sheet A and Sheet B both have a row with matching part numbers and the plant they come from, then A's two cost values are updated to match B's costs.
The next step is then to highlight all cells in Sheet A that are not on Sheet B and highlight all cells in Sheet B that were copied to Sheet A. I think this last part can be done at the same time the cell is being copied I'm just not sure how to do any of this.
This is a formula method.
Because you will not be changing all the values and I assume you want to keep those that do not have a match, then in an empty column next to the figures on sheet 1 put the following formula:
=IFERROR(INDEX(Sheet2!F$3:F$7,MATCH(1,INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:$B$7=$C3),),0)),G3)
Then copy over one column and down the the end of the data.
The INDEX((Sheet2!$D$3:$D$7=$A3)*(Sheet2!$B$3:$B$7=$C3),) will create an array of 0 and 1's the same size as the data reference on sheet 2. In this instance it will create a 1 dimensional array that is 5 objects.
The position of these objects of 0 and 1 are relative to the rows. So for the first formula the return array will be {0,1,0,0,0} because only the second row of the data matches both the plant and the part number.
The MATCH(1,INDEX(...),0) then finds the first object in that array that is 1 and returns the relative position, in this case 2 as it is the second in the array.
The Outer INDEX(Sheet2!F$3:F$7,...) then returns the value in the range Sheet2!F$3:F$7 whose relative position is equal to the 2 passed from the MATCH(). So Sheet2!F4.
If no MATCH is found then the whole thing will throw a #N/A error so we capture that error with IFERROR(...,G3) and tell the formula to return the value in column G instead.
This will give you all the proper values:
Then you can copy and paste just the values back to the original spots and hide the columns with the formulas:
Sheet2 for reference:
If you want vba to do the last part of copy and past and hiding then use the macro recorder and then clean up the code.

vlookup if name is matched display employee id

Ive never used vlookups i have a spreedsheet not sure if this is the right function. I have two sheets
Sheet 1
first name last name username
Sheet 2
first name last name employee id business unit
I need in column D on sheet 1 to have employee id ive below. Pay no attention to column letters and sheets because i moved to another sheet to try getting this right.
=MATCH(B11,Sheet1!C:C,0)
Any help is much appreciated.
So you realize you have to match both first and last names? There are several ways to accomplish this depending on how many employees you have in sheet 2: a) small list could a two-column search using array formula; b) large list just create another column in both sheets joining last & first names and do a MATCH or VLOOKUP on them.
Since your needs are simple and to illustrate option (b):
Insert a column in both Sheet1 and Sheet2 after the "last name"; you should now have an empty column C in both sheets.
Assuming you have column headers in row one, and thus data starts in row two, set cell C2 in both sheets with function =B2&","&A2, then fill-down that formula on both sheets in all rows.
Set Sheet1 cell E2 to formula =VLOOKUP(Sheet1!C2, Sheet2!$C:$D, 2, False), and fill-down that formula in all rows.
Voila, employee IDs on Sheet1. I do have to say this is so Excel 101. There are all sorts of examples and tutorials on this easily found using even the most trivial Google searches.

Excel VBA - selecting the range from first row to the last row

I have a problem with VBA code. I have a sheet with a lot of data around the cells I want. I need to select data in column F, but the position of the first and last cells between the range is to be selected is changing up and down. I created the non empty cells in row X where there is no data do the LastRow function has any refernece but now I dont know how to select the first row
thx for help
If F1 is empty, this selects the first cell with data in the F column
Worksheets("Sheet1").Range("F1").End(xlDown).Select
If F1 may be non-empty you can add a check to see whether it contains text.

Get Unique data from excel by button click?

I am new to excel macros. need some help from you.I have a excel file with 2 sheets.
in sheet one i have some data
In second sheet i need a button which will fetch the distinct C column(Mname) and their ID.
Like most Excel problems, this can be done with a macro, but could also be done with just formulas. Macros are often more trouble than they're worth, so here's the formula option. There are some interesting downsides to using formulas in this way, mainly that if you do too much in a single workbook, calculation will be very slow. But as long as you're dealing with less than a few thousand rows, it shouldn't be a problem.
On your primary worksheet, enter this formula in D2, and then drag it to auto-fill down however many rows you forsee using. When you're done, you can hide column D, if it suits you.
=IF(COUNTIF($C:$C,$C2)<2,MAX($D$1:$D1)+1,0)
The COUNTIF formula looks for how many elements in column C match the contents of $C2 (where $C2 is a (partially) relative reference, so the row number will change when you auto-fill into other rows). If the count is less than 2 (the element in $C2 is unique), then the IF formula returns an index number: 1 for the first unique element in the column, 2 for the second, so on. All cells in column D where the corresponding cell in C is not unique are filled with 0.
On your second sheet, you will fill columns A and B with 'lookup' formulas, searching for positive values in column D of the primary sheet. Most people use VLOOKUP() for lookups, I prefer a combination of INDEX() and MATCH(). And here's a great example of why, since our 'unique index' is not in the first column of the primary sheet (so VLOOKUP wouldn't work!).
=IFERROR(INDEX('Primary Sheet'!$A:$A,MATCH(ROW(A2)-1,'Primary Sheet'!$D:$D,0)),"")
and for the 'Mname' column of the second sheet,
=IFERROR(INDEX('Primary Sheet'!$C:$C,MATCH(ROW(B2)-1,'Primary Sheet'!$D:$D,0)),"")
These lookup formulas are based on their own row number. So the formula in cell A2 is looking for the row in column D of Primary Sheet that contains the unique index 2-1=1. The formula in A3 looks for the index 3-1=2, etc. The IFERROR() formula that is wrapped around the lookup formula ensures that if the sought-after index is not found, the formula returns an empty string (""), which looks like a blank cell. This way you can prepare a large number of rows on your secondary sheet with these lookup formulas. And if there are only a small number of unique Mnames on Primary Sheet, your secondary sheet won't have columns full of #N/A errors.