how to create relationship in vba for excel? - excel

let's call this table1:
|111|222|333|444|555
--------------------
|6 |6 |4 |3 |3
now lets say i have another table, and i need to use the information from both of them,
table2:
111|aaa
222|bbb
333|ccc
444|ddd
555|eee
I want the result table to look like this:
result table:
aaa|6
bbb|6
ccc|4
ddd|3
eee|3
any ideas?

Use a direct reference to get the first column of your results table:
For the second column, use combination of Index, Match, and HLookup functions:

You can use HLOOKUP.
For example, if you first table is in A1:E2 then you can use:
A B C D E
1 111 222 333 444 555
2 6 6 4 3 3
3
4
5 111 aaa =HLOOKUP(A5,$A$1:$E$2,20 //Returns 6
6 222 bbb
7 333 ccc
8 444 ddd
9 555 eee
Just drag down the formula.

Related

Sum rows with same values and write it in new cell

I have the following table:
OrderNumber
Value
123
2
123
3
333
5
333
6
555
8
555
9
My goal is to sum all OrderNumbers with the same values (e.g. for OrderNumber 123 the sum should be 5) and output the result in a new row.
The output should be like this:
OrderNumber
Value
Result
123
2
5
123
3
5
333
5
11
333
6
11
555
8
17
555
9
17
I've seen some formulas beginning with =SUM(A2:A6;A2;B2:B6). Important to me is that the searching criteria must be dynamically because my table has about 1k rows.
Do you have any references or suggestions?
You need SUMIF() function.
=SUMIF($A$2:$A$7,A2,$B$2:$B$7)
If you are a Microsoft 365 user then can try BYROW() for one go.
=BYROW(A2:A7,LAMBDA(x,SUMIF(A2:A7,x,B2:B7)))
This is the exact reason why the "Subtotals" feature has been invented:

Sum up unique values in column per criteria in other column (if values are on the left side of the criteria column)

| A B
---|-----------------------------------------
1 | 1.900
---|-----------------------------------------
2 | 700 Product_A
3 | 700 Product_A
---|-----------------------------------------
4 | 300 Product_B
---|-----------------------------------------
5 | 200 Product_C
6 | 200 Product_C
---|------------------------------------------
7 | 700 Product_D
8 | 700 Product_D
9 | 700 Prodcut_D
10 |
With reference to the answer from this question I wanted to sum up the unique values per product in Cell A1.
Therefore, I tried to go with this formula:
A1 =SUM(INDEX(UNIQUE(IF(SUBTOTAL(2;OFFSET(B2:B9;ROW(B2:B9)-ROW(B2);1;1));A2:B9));;1))
However, as a result now I get 0 instead of 1.900.
I assume the issue results because I have the values on the left side of the criteria column.
Do you have any idea how I need to modify the formula to also make it work in the displayed column order?
All you needed to do was to change the 1 into -1 on the OFFSET():
=SUM(INDEX(UNIQUE(IF(SUBTOTAL(2,OFFSET(B2:B9,ROW(B2:B9)-ROW(B2),-1,1)),A2:B9)),,1))

Get last unique row in Excel

I'm having a table looking like this:
id1 | id2 | dateUpdate
==========================================
aaa | 111 | 2016-01-01
aaa | 111 | 2016-01-02
aaa | 222 | 2016-01-05
aaa | 222 | 2016-01-15
bbb | 333 | 2016-01-05
bbb | 444 | 2016-01-01
ccc | 111 | 2016-01-02
I'd like to get only the latest row for each id1/id2 couple:
id1 | id2 | dateUpdate
==========================================
aaa | 111 | 2016-01-02
aaa | 222 | 2016-01-15
bbb | 333 | 2016-01-05
bbb | 444 | 2016-01-01
ccc | 111 | 2016-01-02
Thanks for your help!
Use Aggregate Function
=AGGREGATE(14, 6, 1/($A$2:$A$99=E2)*($B$2:$B$99=F2)*($C$2:$C$99), 1)
You have to put the unique combinations in column E2 and Column F2 downwards
You have five unique combinations
COLUMN A COLUMN B
aaa 111
aaa 222
bbb 333
bbb 444
ccc 111
And Aggregate Funtion is to be put in column G2 and fill down downwards.
=AGGREGATE(14, 6, 1/($A$2:$A$99=E2)*($B$2:$B$99=F2)*($C$2:$C$99), 1)
Regarding Syntax 14 is for Large value that is the for example larger for first aaa 111 combination that is #02-01-2016#
Next digit 6 in formula is for ignoring errors
You can quickly get the pseudo-Last value using the AGGREGATE function..
This is a standard non-array formula that does NOT require Ctrl+Shift+Enter. AGGREGATE was introduced with Excel 2010.
($A$2:$A$99=E2) checks which value of column A matches with the first unique value of column A of unique combination of A & B
This is converted to an array of logical values which gives an array of True Or False. Dividing 1 by these logical values give either 1 or DIV# error. Similar process is adopted for Column B values mentioned in column F. I have taken data limited to 99 rows. If it more number of rows you change the figure of 99 to a higher figure as per your requirements.
HTH
EDIT
You can also go for pivot table solution as suggested by #Tim Biegeleisen in his comments. Snapshot given here-under illustrates that approach.
id1 and id2 are taken as filter fields and Maximum Value of dateUpdate is chosen in the value field.

Excel, Libreoffice/Openoffice Calc: count 'right' answers

I have a table with students' answers to 20 math problems like this:
A | B | C | D | E |...
------------+-----+-----+-----+-----+...
problem no | 1 | 2 | 3 | 4 |...
------------+-----+-----+-----+-----+...
right answer| 3 | 2 | A | 15 |...
------------+-----+-----+-----+-----+...
student1 | 3 | 4 | A | 12 |...
student2 | 2 | 2 | C | 15 |...
student3 | 3 | 2 | A | 13 |...
Now a need a column that counts the 'right' answers for each student.
I can do it this so: =(IF(D$3=D5;1;0))+(IF(E$3=E5;1;0))+(IF(F$3=F5;1;0))+...
...but it's not the nicest way :)
This is a typical use case for SUMPRODUCT:
A B C D E F G
1 problem no 1 2 3 4
2 right answer 3 2 A 15 right answers per student
3 student1 3 4 A 12 2
4 student2 2 2 C 15 2
5 student3 3 2 A 13 3
Formula in G3:
=SUMPRODUCT($B$2:$E$2=$B3:$E3)
If there are more problem numbers, then the column letters in $E$2 and $E3 have to be increased.
How it works:
SUMPRODUCT takes its inner functions as array formulas. So the $B$2:$E$2=$B3:$E3 becomes a matrix of {TRUE, FALSE, TRUE, FALSE} depending of if $B$2=$B3, $C$2=$C3, $D$2=$D3, $E$2=$E3.
In Libreoffice or Openoffice TRUE is 1 and FALSE is 0. So the SUMPRODUCT sums all TRUEs.
In Excel you have to get the boolean values in numeric context first. So the Formula in Excel will be =SUMPRODUCT(($B$2:$E$2=$B3:$E3)*1).
The formula in Row 3 then can be filled down for all student rows. The $ before the row number 2 ensures that thereby the row of the right answers not changes.
Greetings
Axel

Vlookup a Cell which Contains a Part of Other Cell but not that Straightforward

Hi guys and all Excel gurus, I am stuck with this one excel problem which I cannot solve. I tried using Index, Match, Vlookup but to no avail.
Basically I tried getting Column D displays Value from Column B if the Value of Column C contains part of the value in Column A.
So what I'm dealing with is something kind of like this:
Fixed the table display
+------------------------------------------------------+
| Header Column A Column B Column C Column D |
+------------------------------------------------------+
| Row 1 111 AAA 1111 |
| Row 2 222 BBB 112 |
| Row 3 333 CCC 2225 |
| Row 4 444 DDD 333 |
+------------------------------------------------------+
So my expected result would be:
+------------------------------------------------------+
| Header Column A Column B Column C Column D |
+------------------------------------------------------+
| Row 1 111 AAA 1111 AAA |
| Row 2 222 BBB 112 N/A |
| Row 3 333 CCC 2225 BBB |
| Row 4 444 DDD 333 CCC |
+------------------------------------------------------+
Sorry for the poor table display and explanation. Thanks Guys.
=INDEX($C$2:$C$5, MATCH(1,IF(ISERR(FIND($B$2:$B$5, $D2)),0,1),0))
, where 5 is the last data row. Enter as an array formula (Ctrl+Shift+Enter) in E2, then drag down.
BTW on row 4 it gives CCC, not N/A.

Resources