VBA or PQ or Formula or Pivot Table?

nburaq

Board Regular
Joined
Apr 2, 2021
Messages
222
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi Gents,
I would like to compare two sheets having 3 common dependent in each sheet and would like to see the difference at another sheet or in sheet2 specified column as "Difference" I tried Pivot table and xlookup but didn't work for me and I think this one needs VBA or PQ. I even don't know where to post this question... I attach the screenshots and gonna try to upload the data as well (xlbb crashed) Thanks for help and comments!
mrexcel1.png
mrexcel2.png
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Have you tried SUMIFS or SUMPRODUCT?
No, how is the way to do it? I tried to do it using two pivot tables but no luck.If you can share the solution I will be appreciated
 
Upvote 0
To count the total of Qty in sheet1:
Excel Formula:
=SUMIFS(sheet1!C:C,sheet1!A:A,sheet2!B2,sheet1!B:B,sheet2!C2)
 
Upvote 0
To count the total of Qty in sheet1:
Excel Formula:
=SUMIFS(sheet1!C:C,sheet1!A:A,sheet2!B2,sheet1!B:B,sheet2!C2)
Hi,
Thanks for the solution! it seems SUMSIF calculates the total number of materials used for each item in sheet1 thats why I extracted this formula from column D in sheet 2 like:
Excel Formula:
=D2-SUMIFS(sheet1!C:C,sheet1!A:A,sheet2!B2,sheet1!B:B,sheet2!C2)
 
Upvote 0
A power query solution as an alternative
1. Bring each table into the PQ editor
2. Join the tables with a left inner join
Power Query:
let
    Source = Table.NestedJoin(Table1, {"Item", "Material"}, Table2, {"Item", "Material"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"Qty"}, {"Table2.Qty"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Table2",null,0,Replacer.ReplaceValue,{"Table2.Qty"}),
    #"Inserted Subtraction" = Table.AddColumn(#"Replaced Value", "Subtraction", each [Qty] - [Table2.Qty], Int64.Type)
in
    #"Inserted Subtraction"

Book5
ABCDE
1ItemMaterialQtyTable2.QtySubtraction
22011552514-3
321015528312
424012612404
5322NA000
654212612303
754212613101
854212618202
Merge1
 
Upvote 0
A power query solution as an alternative
1. Bring each table into the PQ editor
2. Join the tables with a left inner join
Power Query:
let
    Source = Table.NestedJoin(Table1, {"Item", "Material"}, Table2, {"Item", "Material"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(Source, "Table2", {"Qty"}, {"Table2.Qty"}),
    #"Replaced Value" = Table.ReplaceValue(#"Expanded Table2",null,0,Replacer.ReplaceValue,{"Table2.Qty"}),
    #"Inserted Subtraction" = Table.AddColumn(#"Replaced Value", "Subtraction", each [Qty] - [Table2.Qty], Int64.Type)
in
    #"Inserted Subtraction"

Book5
ABCDE
1ItemMaterialQtyTable2.QtySubtraction
22011552514-3
321015528312
424012612404
5322NA000
654212612303
754212613101
854212618202
Merge1
Thanks for the solution! yesterday I tried to do this with PQ with inner join but it was giving me null values since PQ could not find to match. Now I see where I made mistake in PQ! Thanks again for the code! Such a great community!
 
Upvote 0
Regarding to my previous problem, I have one more question and if you could help me to solve it I will be so glad;
If I add one more column named "New Material" in sheet2, and this column will check whether the material used with the same item number in sheet2 is already available in sheet1 with the same item number then it will result like this is a new material ordered so "YES" or it is already in sheet1 with registered the same item number so "NO" I solved this partly using XLOOKUP like;
VBA Code:
=XLOOKUP($B2&$C2;Sheet1!$A$2:$A$8&Sheet1!&B2:B8;Sheet1!$C$2:$C$8;"YES";0)

And Sheet1:
Book1
ABC
1Item MaterialQty
2201155251
3210155283
4240126124
5322NA0
6542126123
7542126131
8542126182
Sheet1


Sheet2:
Book1
ABCDEF
1OrderitemMaterialQtyDifferenceNew Material (Y/N)
2102011552543N
3202101552812N
4255421912311N
5352011600111Y
Sheet2


Thanks again all your help!
 
Upvote 0
Ok I am almost close to the solution by myself but having one issue using IFERROR IF AND MATCH functions. I would like match function not to do anything if it sees a value as "NA" in material column in sheet2. so my formula so far:
Excel Formula:
=IFERROR(IF(MATCH($B2&$C2;Sheet1!$A$2:$A$1603&Sheet1!$B$2:$B$1603;0);"NO");"YES")
 
Last edited:
Upvote 0
Ok I am almost close to the solution by myself but having one issue using IFERROR IF AND MATCH functions. I would like match function not to do anything if it sees a value as "NA" in material column in sheet2. so my formula so far:
Excel Formula:
=IFERROR(IF(MATCH($B2&$C2;Sheet1!$A$2:$A$1603&Sheet1!$B$2:$B$1603;0);"NO");"YES")


ok I solved the problem by adding extra IF at the top of the function :) so
Code:
=IF(C2="NA";"NO";IFERROR(IF(MATCH($B2&$C2;Sheet1!$A$2:$A$1603&Sheet1!$B$2:$B$1603;0);"NO");"YES"))
 
Upvote 0
Solution

Forum statistics

Threads
1,215,417
Messages
6,124,787
Members
449,188
Latest member
Hoffk036

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top