updating code match three columns toghether for each item and add a new data

KalilMe

Active Member
Joined
Mar 5, 2021
Messages
354
Office Version
  1. 2016
Platform
  1. Windows
hi

earlier @maabadi helped me in this thread match data based on three columns together and put the values into last empty columns
about match between two sheets based on column B,C,D and populate the values in last columns purchase ,sales . every time run the macro .

now I add category for each items in COL A starts from first row to last empty cell berfore start another item in COL A . so in sheet 2 should match COL B,C,D for each item in COL A between two sheets . if there are new data based on COL B,C,D in sheet1 but not existed in sheet2 then should search theses data based on category in COL A and add before row TOTAL with the same stracture without effect the formulas and formatting . so I highlight new data should add to sheet 2 by red
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
sheet1
pop.xlsm
ABCDEF
1CATEGORYCOMMIDETYTYPEORIGINPURCHASESALES
2FFR-100FRBANANASO555
3FRBANANASO110
4FRBANANASO211
5VEG-100VEGTOMATOEG5
6FTT-1FO1TUNE160GSP225
7FO1TUNE160GPO1010
8FO1TUNE160GSPL5-
9FO2TUNE160GSPL6-
SHEET1




sheet2 before
pop.xlsm
ABCDEFG
1CATEGORYCOMMIDETYTYPEORIGINPURCHASESALES BALANCE
2FFR-100FRBANANASO0
3FRBANANASO10
4TOTAL000
5FFT-1FO1TUNE160GSP0
6FO1TUNE160GPO0
7FO1TUNE160GSPL0
8TOTAL000
9VEG-100VEGTOMATOEG0
10TOTAL000
SHEET2
Cell Formulas
RangeFormula
E10:G10,E8:G8,E4:G4E4=SUM(E2:E3)
G9,G5:G7,G2:G3G2=IFERROR(E2-F2,IF(ISNUMBER(E2),E2,F2*-1))



sheet 2 after
pop.xlsm
ABCDEFG
1CATEGORYCOMMIDETYTYPEORIGINPURCHASESALES BALANCE
2FFR-100FRBANANASO55550
3FRBANANASO110010
4FRBANANASO21111
5TOTAL76571
6FFT-1FO1TUNE160GSP22517
7FO1TUNE160GPO10100
8FO1TUNE160GSPL5-5
9FO2TUNE160GSPL6-6
10TOTAL211011
115VEGTOMATOEG55
12TOTAL505
EXPECTED RESULT
Cell Formulas
RangeFormula
E10:G10,E5:G5E5=SUM(E2:E4)
G11,G6:G9,G2:G4G2=IFERROR(E2-F2,IF(ISNUMBER(E2),E2,F2*-1))
E12:G12E12=SUM(E11)
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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