VBA macro to combine two sheets on unique id - excel

I have two sheets in Excel workbook.
The first sheet has
1) Customer ID – unique values for each customer.
2) Question ID – unique id for each question
3) Questions
Customer ID Question ID question
1 34 name
1 45 company
2 34 name
2 45 company
3 34 name
3 45 company
4 34 name
4 45 company
5 34 name
5 45 company
The second sheet has three columns
1) Customer ID – unique values for each customer.
2) Question ID – unqiue id for each question
3) Questions
Customer ID Question ID Answer
1 34 Amy
1 45 GEICO
2 34 Steph
3 34 Anna
3 45 GEICO
4 34 Adam
5 34 Mark
5 45 AAA
In this sheet, not every customer id and Question ID in sheet one will have answers in the sheet 2
Sheet 3 Expected Output
I wanted to do a vba macro to combine both sheet1 and sheet2 and have all the columns. For any customer id, if there is no answer for a question, that field should be left blank.
Expected Output in Sheet3
Customer ID Question ID question Answer
1 34 name Amy
1 45 company GEICO
2 34 name Steph
2 45 company
3 34 name Anna
3 45 company GEICO
4 34 name Adam
4 45 company
5 34 name Mark
5 45 company AAA

There are several ways this can be done without writing code.
Below is one method off the top of my head. Others include the built-in query editor (Get & Transform), or PivotTables and others ways to consolidate data in multiple worksheets.
On Sheet2, first set up a "helper column" since there are multiple columns you want to match. In this example the formula is: =C2&D2 starting in Cell B2.
...then, in Sheet1 (cell E2 in the example), use a formula like:
=IFERROR(VLOOKUP(B2&C2,Sheet2!$B$1:$E$9,4,FALSE),"")
Both formulas get copied or "dragged" down as far as necessary and obviously the formula adjusted to refer to the correct cells.
No third worksheet is necessary but if you want you can start by copying Sheet1 to Sheet3.
More Information:
Microsoft Support : VLOOKUP Function
Microsoft Support : Lookup & Reference Functions
Microsoft Support : IFERROR Function

Related

Updating spreadsheet after altering the original one

I have the following data in spreadsheet A.
name trait1 trait2 nice
0 Adam 29 81 0
1 Barry 17 75 1
2 Chris 62 0 1
I wish to create a spreadsheet B that will be a filtered copy of this data. Namely, let's assume for a moment that I want to filter nice = 1 and am interested only in column name. The copy in spreadsheet B would be as shown below. In spreadsheet B I wish to be adding some extra columns, e.g. education.
name nice education
1 Barry 1 primary
2 Chris 1 university
What I want to achieve is a spreadsheet B that will get updated if anything changes in spreadsheet A. So for example, if I were to change the name Barry to Ben. The spreadsheet B would become the following.
name nice education
1 Ben 1 primary
2 Chris 1 university
Similarly (and what I find to be the hardest), if a row is added in spreadsheet A, e.g.
name trait1 trait2 nice
0 Adam 29 81 0
1 Barry 17 75 1
2 Matt 69 11 1
3 Chris 62 0 1
The updated spreadsheet B would be as follows:
name nice education
1 Barry 1 primary
2 Matt 1
3 Chris 1 university
So I want the education column to remain the same.
My approach of using a combination of =IF() and =VLOOKUP() functions ultimately did not work. Guess I am really curious about how to connect rows of education to names. So that when a row is added in spreadsheet A, then spreadsheet B gets updated but the education field connected to the new row is empty and will be filled by hand later on.
Since you are looking for a finished product to be in Google Sheets, I'd advise to use QUERY():
Formula in I1:
=QUERY(INDEX({A:D,VLOOKUP(A:A,F:G,2,0)}),"Select Col1,Col4,Col5 where Col4=1")
Note: I made the assumption you pull the education in through a VLOOKUP() (since you mentioned that in the body of the question).

How to combine SUMPRODUCT with an INDEX and MATCH formula?

Note, I have edited my original question to clarify my problem:
As the title suggests, I am looking for a way to combine the SUMPRODUCT functionalities with an INDEX and MATCH formula, but if a better approach exists to help solve the problem below I am also open to it.
In the below example, imagine that the tables are on different sheets. I have a report that has the sales of each ID in the rows and each month in the columns (first table). Unfortunately, the report only has IDs and not the region they belong to, but I do have a look up table which labels each ID with their respective region (second table):
A
B
C
D
1
ID
January
February
March
2
1
10
5
20
3
3
5
5
10
4
7
0
10
5
5
14
10
25
5
6
25
5
10
10
7
27
10
10
10
8
44
5
5
5
A
B
1
ID
Region
2
1
East
3
3
East
4
7
Central
5
14
Central
6
25
Central
7
27
West
8
44
West
My goal is to be able to aggregate the sales by region as per the result below. However I would only like to show sales data that belong to the month that is shown in cell D2.
Goal:
A
B
C
D
1
Region
Sales
February
2
East
10
3
Central
45
4
West
15
I have used the INDEX and MATCH combination to return a single value, but not sure how I can return multiple values with it and aggregate them at the same time. Any insight would be appreciated!
You may just use:
=SUMPRODUCT((Sheet1!B$1:D$1=D$1)*(Sheet1!H$2:H$8=A2),Sheet1!B2:D8)
Remember, SUMPRODUCT() could be quite heavy processing huge data, therefor to combine INDEX() and MATCH() is not a bad idea, but let's do it the other way around and nest the latter two into SUMPRODUCT() instead =):
=SUMPRODUCT(INDEX(Sheet1!B$2:D$8,0,MATCH(D$2,Sheet1!B$1:D$1,0))*(Sheet1!H$2:H$8=A2))
Another option using SUMIF+INDEX+MATCH function as in
In "Sheet2" B2, copied down :
=SUMIF(Sheet1!H:H,A2,INDEX(Sheet1!B$1:D$1,MATCH(D$2,Sheet1!B$1:D$1,0)))

How to check if a record exists based on month and persons name (where the name is duplicated)?

I have a data-set with some names and months, sample:
Name Month
Max 2
Sally 5
Max 1
James 11
Richard 9
Sally 9
I then have a table as such:
Month
Name 1 2 3 4 5 6 7 8 9 10 11 12
Max
Sally
James
Richard
How do I create a formula that can enter "Yes", or "No" for each month based on the person's name? For example, Max would have months 2 and 1 displaying "Yes", all other months displaying "No".
I can't think of how to do this...using an INDEX/MATCH function I can pull up the first month for "Max", but it won't register the second time his name shows up (it just matches the order of his name with where his data lies in the array, doesn't consider duplicates).
Is there a way to do this?
Thanks.
Assuming a data setup like this:
In cell E3 and copied over and down is this formula:
=IF(COUNTIFS($A$2:$A$7,$D3,$B$2:$B$7,E$2),"Yes","No")

Formula Excel Vlookup

I have my main sheet.
Account Card
1 22
1 33
Same person has 2 cards. Hence, the balance is same.
Vlookup sheet has data like below.
Account Balance
1 5
When I do the following:
=VLOOKUP(A2,DATA!A2:B84520,2,FALSE)
It returns value for first Account 1. and N/A for second.
Account Card Balance
1 22 5
1 33 #N/A
How can I fix the formula?
Thanks
Put $ in A$2:B$84520. Try.
Best
Anderson

How can I merge rows in Excel 2010?

My Excel sheet contains columns for id, date, amount and name.
If two or more rows have the same id and name I want to merge them into one row with the amount of those two combined. Can anybody help me doing this?
Use concatenation with Columns ID and NAME (=ID&NAME) in another column and look for the pivot table. Move to Insert->Pivot table option to get the attached answer]
Hope this clarifies
Here is a possible solution (as far as I understand the problem) using a PivotTable:
As you can see, there is not really much to it. Just insert a pivot table and drag the ID & Name to the Rows while you move the dates and the values to the Values section of the pivot table. The Values in the Column section will be automatically generated.
The only thing I changed at the end was to format the pivot table nicely doing the following:
Remove SubTotals by name
Using a PivotTable style that I like from the PivotTable menu in the section Design.
Show items in a tabular format. That means that the name and the ID are shown on one row and not in a cascading format (ID in one row and the name in a second row underneath + indented). You can achieve this by right-clicking on the name and selecting Field Settings. A new window opens (showing the settings). Go here to the Layout & Print tab and select Show item labels in a tabular form. You might have to repeat that for the field Name and the field ID (depending on which of the two fields you chose to show first in the Rows section of the PivotTable).
Let me know if this solved your problem or if you have any questions.
Note: since no sample data has been provided I made some up. I sure hope this data is somewhat similar to what you are trying to aggregate.
Used sample data:
ID Date Amount Name
1 05.03.2015 121 Peter
2 14.01.2015 127 Max
3 11.01.2015 77 Eva
4 06.11.2015 141 Jamie
5 04.03.2015 58 Sophie
1 04.04.2015 94 Peter
7 20.06.2015 52 Lucas
3 14.12.2015 136 Eva
2 02.11.2015 93 Max
10 20.08.2015 59 Mason
4 11.01.2015 145 Jamie
4 02.02.2015 113 Jamie
4 26.02.2016 89 Jamie
1 30.03.2016 137 Peter
15 09.09.2015 81 Jackson
16 25.02.2015 61 Liam
2 26.02.2016 133 Max
2 26.10.2015 80 Max
19 25.09.2015 76 Aiden

Resources