How to compare 2 values between 2 tables in 2 different sheets and bring the value from the second table to the first one.

gagasp

New Member
Joined
Jun 2, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
I have these codes from a prior post which were given by a generous contributor. Since I am new to VBA I need to seek further help to have the loop comparing aList (sheet1) with aComplex (Sheet2) (Please note that now they are working if in the same sheet) and then add the values found on aComplex loop into the dictionary in sheet1.
Thanks in advance for the help
gio

VBA Code:
Sub Shoppinglist()
     Dim Dict, aComplex, aList1
     Set Dict = CreateObject("scripting.dictionary")     'your shoppinglist
     Dict.comparemode = vbTextCompare     'is case insensitive

     With Sheets("blad1")     'this worksheet
          aComplex = .ListObjects("TBL_Complex").DataBodyRange.Value     'the complex ingredients
          aList1 = .ListObjects("TBL_List1").DataBodyRange.Value     'the initial shoppinglist with simple and complex ingredients
          For i = 1 To UBound(aList1)     'loop throug that list
               If aList1(i, 3) <> "x" Then     '3rd column not "x" = simple ingredient
                    Dict.Add Dict.Count, Array(aList1(i, 1), aList1(i, 2), aList1(i, 3), aList1(i, 4))     'copy then index, naam, unit and amount to the shoppinglist
               Else     'it's a complex ingredient
                    For j = 1 To UBound(aComplex)     'loop through the complex list
                         If aComplex(j, 1) = aList1(i, 2) Then     'same name
                              Dict.Add Dict.Count, Array(aList1(i, 1), aComplex(j, 2), aComplex(j, 3), aComplex(j, 4) * aList1(i, 4))     'add index, complex subname, complex subunit, complex amount multiplied by the initial amount
                         End If
                    Next
               End If
          Next

          i = Dict.Count     'number of lines
          With .ListObjects("TBL_List2")     'the extended list
               If .ListRows.Count Then .DataBodyRange.Delete     'start empty if it wasn't
               If i > 0 Then     'there are items
                    If i = 1 Then Dict.Add Dict.Count, Array("", "", "", "")     'bug when only 1 item, add an empty row with equal elements
                    arr = Application.Index(Dict.items, 0, 0)     'read items to an array
                    .ListRows.Add.Range.Range("A1").Resize(i, UBound(arr, 2)).Value = arr     ' write array to table
               End If
               .HeaderRowRange.EntireColumn.AutoFit
          End With

          ThisWorkbook.RefreshAll
          .PivotTables(1).TableRange1.EntireColumn.AutoFit

     End With


End Sub
 
Last edited by a moderator:

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

Forum statistics

Threads
1,223,715
Messages
6,174,065
Members
452,542
Latest member
Bricklin

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