match data based on three columns together and put the values into last empty columns

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
354
Office Version
  1. 2016
Platform
  1. Windows
hi
I have two sheets should match sheet2 with sheet1 based on columns B,C,D toghether and copy the values to last two empty columns contain headers (purchase and sales) .so in this case after match the data between two sheets it will populate the values in COL E,F in sheet2 and if I run macro again will copy to next empty columns PURCHASE , SALES (H,I) an so on . every time run the macro should search for empty columns(PURCHASE , SALES) and populate the values based on what is existed in sheet1.
I put the expected result should be in sheet2 after matching with sheet1 . with considering the data in sheet1 are increasable and the inserted columns also are increasable in sheet2

sheet1
pop.xlsm
ABCDEF
1DATECOMMIDETYTYPEORIGINPURCHASESALES
21/7/2021FRBANANASO555
31/8/2021FRBANANASO110
41/10/2021VEGTOMATOEG5
51/17/2021FO1TUNE160GSP225
61/18/2021FO1TUNE160GPO1010
71/18/2021FO1TUNE160GSPL5-
SHEET1




sheet2 before
pop.xlsm
ABCDEFGHIJKLMNOP
1ITEMCOMMIDETYTYPEORIGINPURCHASESALES BALANCEPURCHASESALES BALANCEPURCHASESALES BALANCEPURCHASESALES BALANCE
21FRBANANASO0000
32FRBANANASO10000
43FO1TUNE160GSP0000
54FO1TUNE160GPO0000
65VEGTOMATOEG0000
76FO1TUNE160GSPL0000
SHEET2
Cell Formulas
RangeFormula
P2:P7,M2:M7,J2:J7,G2:G7G2=E2-F2



sheet2 after
pop.xlsm
ABCDEF
1ITEMCOMMIDETYTYPEORIGINPURCHASESALES
21FRBANANASO555
32FRBANANASO110
43FO1TUNE160GSP225
54FO1TUNE160GPO1010
65VEGTOMATOEG5
76FO1TUNE160GSPL5
EXPECTED RESULT
 
Then Try this:
VBA Code:
Sub Macro2()
Dim Lr1 As Long, Lr2 As Long, Lc As Long, j As Long
Lr1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Lc = Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Column
If Range("E2").Value = "" Then
Range("E2:F" & Lr2).Formula = "=INDEX(Sheet1!E$2:E$" & Lr2 & " ,MATCH(1,INDEX((Sheet1!$B$2:$B$" & Lr2 _
& " =$B2)*(Sheet1!$C$2:$C$" & Lr2 & " =$C2)*(Sheet1!$D$2:$D$" & Lr2 & " =$D2),0,1),0),1)"
Range("E2:F" & Lr2).Value = Range("E2:F" & Lr2).Value

Else
For j = 8 To Lc Step 3
If Cells(2, j).Value = "" Then
Range(Cells(2, j), Cells(Lr2, j + 1)).Formula = "=INDEX(Sheet1!E$2:E$" & Lr2 & " ,MATCH(1,INDEX((Sheet1!$B$2:$B$" & Lr2 _
& " =$B2)*(Sheet1!$C$2:$C$" & Lr2 & " =$C2)*(Sheet1!$D$2:$D$" & Lr2 & " =$D2),0,1),0),1)"
Range(Cells(2, j), Cells(Lr2, j + 1)).Value = Range(Cells(2, j), Cells(Lr2, j + 1)).Value
Exit For
End If
Next j

End If

End Sub
 
Upvote 0
Solution

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
well done ! this is exactly what I want . just last thing can you mod the code and make any empty cell should be hyphen "-" and without effect the subtracting . it should n't show error when subtract in COL BALANCE
 
Upvote 0
Try this:
VBA Code:
Sub Macro2()
Dim Lr1 As Long, Lr2 As Long, Lc As Long, j As Long
Lr1 = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Lr2 = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
Lc = Sheets("Sheet2").Cells(1, Columns.Count).End(xlToLeft).Column
If Range("E2").Value = "" Then
Range("E2:F" & Lr2).Formula = "=INDEX(Sheet1!E$2:E$" & Lr2 & " ,MATCH(1,INDEX((Sheet1!$B$2:$B$" & Lr2 _
& " =$B2)*(Sheet1!$C$2:$C$" & Lr2 & " =$C2)*(Sheet1!$D$2:$D$" & Lr2 & " =$D2),0,1),0),1)"
Range("G2:G" & Lr2).Formula = "=IFERROR(E2-F2,""-"")"
Range("E2:F" & Lr2).Value = Range("E2:F" & Lr2).Value

Else
For j = 8 To Lc Step 3
If Cells(2, j).Value = "" Then
Range(Cells(2, j), Cells(Lr2, j + 1)).Formula = "=INDEX(Sheet1!E$2:E$" & Lr2 & " ,MATCH(1,INDEX((Sheet1!$B$2:$B$" & Lr2 _
& " =$B2)*(Sheet1!$C$2:$C$" & Lr2 & " =$C2)*(Sheet1!$D$2:$D$" & Lr2 & " =$D2),0,1),0),1)"
Range("G2:G" & Lr2).Copy Range(Cells(2, j + 2), Cells(Lr2, j + 2))
Range(Cells(2, j), Cells(Lr2, j + 1)).Value = Range(Cells(2, j), Cells(Lr2, j + 1)).Value
Exit For
End If
Next j
End If
End Sub
 
Upvote 0
thanks
but it shows a problem . I mean this - should replace of the empty cell . the empty cell considers 0 . so when in COL PURCHASE is 5 and COL SALES "-" then the COL BALANCE should be 5 but your code shows" -" in COL BALANCE . may you fix it , please ?
 
Upvote 0
Why you don't Replace "-" at the Purchase or Sale Col with 0?
Replace this line
VBA Code:
Range("G2:G" & Lr2).Formula = "=IFERROR(E2-F2,""-"")"
With
VBA Code:
Range("G2:G" & Lr2).Formula = "=IFERROR(E2-F2,IF(ISNUMBER(E2),E2,F2*-1))"
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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