put the QTY to adjacent ID based on arranged sheet when compare two sheets

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
651
Office Version
  1. 2019
Hello

I would populate data in RESULT sheet based on arranged data in PR sheet . should brings data columns D,E from PR sheet and brings QTY from STOCK sheet and put to adjacent cell for brought QTY from PR sheet based on matching BRAND column between two sheets.
I have about 3000 items for each sheet .
this is simple data
av.xlsm
ABCDEFG
1DATE CLIENT NOINVOICE NOBRANDQTYUNIT PRICETOTAL
201/05/2021CUS-BS-1INV-BS-11200R20 G580 JAP1,000.002,500.002,500,000.00
301/06/2021CUS-BS-1INV-BS-11200R20 G580 THI10.002,660.0026,600.00
401/08/2021CUS-BS-1INV-BS-11200R20 R187 THI10.002,450.0024,500.00
501/09/2021CUS-BS-2INV-BS-21200R24 G580 JAP5.003,000.0015,000.00
601/12/2021CUS-BS-2INV-BS-21400R20 VSJ JAP12.004,000.0048,000.00
702/12/2021CUS-BS-3INV-BS-31400R20 R180 JAP11.004,000.0044,000.00
PR
Cell Formulas
RangeFormula
G2:G7G2=E2*F2


av.xlsm
ABCDE
1ITEMBRANDREF NOBATCHQTY
211200R20 R187 THIREF D#LLBVGT6024,000.00
321400R20 VSJ JAPREF D#SSBVGT6041,000.00
431200R24 G580 JAPREF D#KKBVGT6035,000.00
541200R20 G580 JAPREF D#NNBVGT6002,000.00
651200R20 G580 THIREF D#MMBVGT6013,000.00
761200R24 G580 THIREF D#KKBVGT6035,000.00
STOCK




should be
av.xlsm
ABCD
1ITEMBRANDQTYQTY1
211200R20 G580 JAP1,000.002,000.00
321200R20 G580 THI10.003,000.00
431200R20 R187 THI10.004,000.00
541200R24 G580 JAP5.005,000.00
651400R20 VSJ JAP12.001,000.00
761400R20 R180 JAP11.000.00
871200R24 G580 THI0.005,000.00
RESULT
 
Last edited:

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
A Power Query solution

Power Query:
let
    T1 = Excel.CurrentWorkbook(){[Name="PR"]}[Content],
    T2 = Excel.CurrentWorkbook(){[Name="Stock"]}[Content],
    MQ = Table.NestedJoin(T1, {"BRAND"}, T2, {"BRAND"}, "Table2", JoinKind.FullOuter),
    #"Renamed Columns" = Table.RenameColumns(MQ,{{"QTY", "QTY1"}}),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Renamed Columns", "Table2", {"BRAND", "QTY"}, {"BRAND.1", "QTY"}),
    #"Renamed Columns1" = Table.RenameColumns(#"Expanded Table2",{{"QTY", "QTY2"}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns1",{"BRAND", "QTY1", "BRAND.1", "QTY2"}),
    #"Added Custom" = Table.AddColumn(#"Removed Other Columns", "Brand.2", each if[BRAND]= null then [BRAND.1] else [BRAND]),
    #"Removed Other Columns1" = Table.SelectColumns(#"Added Custom",{"QTY1", "QTY2", "Brand.2"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Removed Other Columns1",{"Brand.2", "QTY1", "QTY2"})
in
    #"Reordered Columns"

Book1
ABC
10Brand.2QTY1QTY2
111200R20 G580 JAP10002000
121200R20 R187 THI104000
131200R20 G580 THI103000
141400R20 VSJ JAP121000
151200R24 G580 JAP55000
161200R24 G580 THI5000
171400R20 R180 JAP11
Sheet1
 
Upvote 0
FYI in case you are not familiar:

Power Query is a free AddIn for Excel 2010 and 2013, and is built-in functionality from Excel 2016 onwards (where it is referred to as "Get & Transform Data").

It is a powerful yet simple way of getting, changing and using data from a broad variety of sources, creating steps which may be easily repeated and refreshed. I strongly recommend learning how to use Power Query - it's among the most powerful functionalities of Excel.

- Follow this link to learn how to install Power Query in Excel 2010 / 2013.

- Follow this link for an introduction to Power Query functionality.

- Follow this link for a video which demonstrates how to use Power Query code provided.
 
Upvote 0
Hi @abdo meghari

Try this:

VBA Code:
Sub put_QTY()
  Dim sh1 As Worksheet, sh2 As Worksheet, sh3 As Worksheet
  Dim a As Variant, b As Variant, c As Variant
  Dim dic1 As Object, dic2 As Object
  Dim i As Long, n As Long
  
  Application.ScreenUpdating = False
  
  Set sh1 = Sheets("PR")
  Set sh2 = Sheets("STOCK")
  Set sh3 = Sheets("RESULT")
  Set dic1 = CreateObject("Scripting.Dictionary")
  Set dic2 = CreateObject("Scripting.Dictionary")
  
  a = sh1.Range("A2", sh1.Range("E" & Rows.Count).End(3)).Value
  b = sh2.Range("A2", sh2.Range("E" & Rows.Count).End(3)).Value
  ReDim c(1 To UBound(a, 1) + UBound(b, 1), 1 To 4)
  
  For i = 1 To UBound(a, 1)   'PR
    dic1(a(i, 4)) = a(i, 5)
  Next
  
  For i = 1 To UBound(b, 1)   'STOCK
    dic2(b(i, 2)) = b(i, 5)
  Next
  
  n = 0
  For i = 1 To UBound(a, 1)   'PR
    n = n + 1
    c(n, 1) = n
    c(n, 2) = a(i, 4)
    c(n, 3) = a(i, 5)
    If Not dic2.exists(a(i, 4)) Then c(n, 4) = 0 Else c(n, 4) = dic2(a(i, 4))
  Next
  
  For i = 1 To UBound(b, 1)   'STOCK
    If Not dic1.exists(b(i, 2)) Then
      n = n + 1
      c(n, 1) = n
      c(n, 2) = b(i, 2)
      c(n, 3) = 0
      c(n, 4) = b(i, 5)
    End If
  Next
  
  sh3.Range("A1:D1").Value = Array("ITEM", "BRAND", "QTY", "QTY1")
  sh3.Range("A2:E" & Rows.Count).ClearContents
  sh3.Range("A2").Resize(UBound(c, 1), UBound(c, 2)).Value = c
  sh1.Range("E1").Copy
  sh3.Range("A1:D1").PasteSpecial xlPasteFormats
  sh1.Range("D2").Copy
  sh3.Range("A2:B" & n + 1).PasteSpecial xlPasteFormats
  sh1.Range("E2").Copy
  sh3.Range("C2:D" & n + 1).PasteSpecial xlPasteFormats
  
  Application.CutCopyMode = False
  Application.ScreenUpdating = True
End Sub

🤗
 
Upvote 0
Solution

Forum statistics

Threads
1,224,817
Messages
6,181,149
Members
453,021
Latest member
Justyna P

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