Moving Data between Worksheets based on a key - vba

In both worksheets I have a key in column A. Not all of the key values are in both worksheets.
For each key value in column A of Worksheet 1, I want to find the corresponding key in worksheet 2 and move the data in column B of worksheet 2 into column B of worksheet 1.
I have never programmed a macro so I am completely lost for writing this code.

There is no need for VBA here, Philip.
Try this formula in cell B1 of Sheet1:
=IFERROR(vlookup(A1,Sheet2!A:B,2,false),"")
If you are on XL 2003, then do this:
=IF(ISERROR(vlookup(A1,Sheet2!A:B,2,false)),"",vlookup(A1,Sheet2!A:B,2,false))
Then drag this formula down for the rest of your data in Column A of Sheet1.
NB - Sheet2 is the assumed name of your Worksheet2, you may need to change to fit your needs.
NB2 - you can lookup vlookup in Excel Help (or online) to better understand what it does

Are you sure you need to use a macro for this? It sounds to me like you are using Excel and a simple VLOOKUP formula would do what you need.
A VLOOKUP looks like this: =VLOOKUP(A1,Sheet2!$A:$B,2,FALSE)
Where A1 is your reference cell (in this case you "key" in column A).
Sheet2!$A:$B is the lookup table (in this case columns A and B from the second sheet).
2 is the column number that you want data from, counting from the left (in this case the second column, column B)
FALSE tells the formula to only return values for exact matches. TRUE returns the closest match in the lookup table.

Related

Pulling Data Between Excel Worksheets

I have two excel worksheets and I am looking to pull data from one worksheet column based on another column in the same worksheet that contains a unique identifier (name).
I am planning to create two cells in worksheet one that would allow the unique identifier to be entered in one cell (ex. name), and it would pull the data from the other column (time) to display in the other cell.
Worksheet 1 Worksheet 2
Name Cell 1: Name Column, Time Column
Time Cell 2: (displayed after entering name)
Is there an excel function or sql function to accomplish this? Thank you.
Have you tried a vlookup function already?
https://www.excel-easy.com/examples/vlookup.html

Excel - Have a value from column B of a 2 column reference pasted anytime the value from column A is entered on a separate worksheet

In the example I have a 2 column reference where data in column E is correlated to column F. I need the data in Column F to show up anytime data from Column E is entered in a separate worksheet if possible.
To be clear, is it possible to enter data in Column A of Sheet 1 that pulls the correlated data from column B in reference Sheet 2 and enters it into Column B of Sheet 1?
I tried using a simple function in a single sheet but it obviously doesn't do what I need.
You are after VLOOKUP or INDEX MATCH
Sheet1 b1 and fill down rows
=IFERROR(VLOOKUP(A1,Sheet2!E:F,2,FALSE),TEXT(,))
Or
=IFERROR(INDEX(Sheet2!F:F,MATCH(A1,Sheet2!E:E,0)),TEXT(,))
If you set you data up as tables the formulas will autofill down. Rather than use entire columns you can set to the ranges containing data.
Data:

look for Column heading and sum up

I have been working with a excel file with a lot of data, which is arranged in 2 sheets.
I would like to get data from sheet 1 to sheet 2 with reference to the column headings.
For example:
So if I want to find the sum of function 1 person A with criteria 1, the command have to go and find the heading "sum of function 1" in sheet 1 and choose the data that are only under criteria 1 and sum it up in sheet 2 cell D5. (By using column heading reference instead of cell reference).
The table range is A2 : U80.
Thanks.
First you have to format your data as table (select the data -> Menu Insert -> Table). Then you rename your table, for example Table1. Let's say one of the columns you want to sum on the sheet2 is called ColumnName.
On the sheet 2 you write a formula
=SUM(Table1[ColumnName])
The result will be what you are after.
You should try it by SUMIFS(). Syntax will be
=SUMIFS(AgeRange from sheet1,NameRange Sheet1, Name cell Sheet2, PlaceRangeSh1, Place Cell Sh2)
Tell me if requires further help.

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.

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.