match data between two sheets based on four columns instead of one column

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
match data between two sheets based on four columns instead one column
hi
I have this code works very well . it match data between two sheets based on COL A and update the quantity in sheet inventory after matching it will subtracting the value in COL E from sheet sales .
so what I want update the code to become matching based on COL B,C,D,E together and update the quantity in COL F in sheet INVENTORY as in the code
NOTE: the structure into two sheets are the same thing



VBA Code:
Sub qtyin()
Dim X, cell As Range
With Sheets("Sales")
    For Each cell In .Range("A20:A" & .Cells(.Rows.Count, 1).End(xlUp).Row)
        With Sheets("Result")
            X = Application.Match(cell, .Range("A:A"), 0)
            If Not IsError(X) Then
                .Range("E" & X) = .Range("E" & X).Value - cell.Offset(, 4).Value
            End If
        End With
    Next cell
End With
End Sub


sheet sale
update q.xlsm
ABCDEF
19itemcctbrandtypeoriginquantity
201c-foodtune180wgthi1
212c-wattune180wgindo2
223c-foodtune180wgindo1
SALES



update q.xlsm
ABCDEF
1itemcctbrandtypeoriginquantity
21c-foodtune180wgthi234
32c-wattune180wgindo123
43c-foodtune180wgindo200
inventory
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Try this:

VBA Code:
Sub update_Inventory()
  Dim sh As Worksheet
  Dim dic As Object
  Dim a As Variant
  Dim c As Range
  Dim s As String
  Dim i As Long
  
  Set sh = Sheets("Inventory")
  Set dic = CreateObject("Scripting.Dictionary")
  a = sh.Range("B1:E" & sh.Range("B" & Rows.Count).End(3).Row).Value
  
  For i = 2 To UBound(a, 1)
    dic(a(i, 1) & "|" & a(i, 2) & "|" & a(i, 3) & "|" & a(i, 4)) = i
  Next
  
  For Each c In Sheets("Sales").Range("B2", Sheets("Sales").Range("B" & Rows.Count).End(xlUp))
    s = c.Value & "|" & c.Offset(, 1).Value & "|" & c.Offset(, 2).Value & "|" & c.Offset(, 3).Value
    If dic.exists(s) Then
      i = dic(s)
      sh.Range("F" & i).Value = sh.Range("F" & i).Value - c.Offset(, 4)
    End If
  Next
End Sub
 
Upvote 0
Solution
fantastic ! may you make the code in worksheet event change in sheet sale , then automatically change the QTY in sheet inventory instead of use button
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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