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
sheet1
sheet2 before
sheet 2 after
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
pop.xlsm | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | CATEGORY | COMMIDETY | TYPE | ORIGIN | PURCHASE | SALES | ||
2 | FFR-100 | FR | BANANA | SO | 55 | 5 | ||
3 | FR | BANANA | SO1 | 10 | ||||
4 | FR | BANANA | SO2 | 11 | ||||
5 | VEG-100 | VEG | TOMATO | EG | 5 | |||
6 | FTT-1 | FO1 | TUNE160G | SP | 22 | 5 | ||
7 | FO1 | TUNE160G | PO | 10 | 10 | |||
8 | FO1 | TUNE160G | SPL | 5 | - | |||
9 | FO2 | TUNE160G | SPL | 6 | - | |||
SHEET1 |
sheet2 before
pop.xlsm | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | CATEGORY | COMMIDETY | TYPE | ORIGIN | PURCHASE | SALES | BALANCE | ||
2 | FFR-100 | FR | BANANA | SO | 0 | ||||
3 | FR | BANANA | SO1 | 0 | |||||
4 | TOTAL | 0 | 0 | 0 | |||||
5 | FFT-1 | FO1 | TUNE160G | SP | 0 | ||||
6 | FO1 | TUNE160G | PO | 0 | |||||
7 | FO1 | TUNE160G | SPL | 0 | |||||
8 | TOTAL | 0 | 0 | 0 | |||||
9 | VEG-100 | VEG | TOMATO | EG | 0 | ||||
10 | TOTAL | 0 | 0 | 0 | |||||
SHEET2 |
Cell Formulas | ||
---|---|---|
Range | Formula | |
E10:G10,E8:G8,E4:G4 | E4 | =SUM(E2:E3) |
G9,G5:G7,G2:G3 | G2 | =IFERROR(E2-F2,IF(ISNUMBER(E2),E2,F2*-1)) |
sheet 2 after
pop.xlsm | |||||||||
---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | |||
1 | CATEGORY | COMMIDETY | TYPE | ORIGIN | PURCHASE | SALES | BALANCE | ||
2 | FFR-100 | FR | BANANA | SO | 55 | 5 | 50 | ||
3 | FR | BANANA | SO1 | 10 | 0 | 10 | |||
4 | FR | BANANA | SO2 | 11 | 11 | ||||
5 | TOTAL | 76 | 5 | 71 | |||||
6 | FFT-1 | FO1 | TUNE160G | SP | 22 | 5 | 17 | ||
7 | FO1 | TUNE160G | PO | 10 | 10 | 0 | |||
8 | FO1 | TUNE160G | SPL | 5 | - | 5 | |||
9 | FO2 | TUNE160G | SPL | 6 | - | 6 | |||
10 | TOTAL | 21 | 10 | 11 | |||||
11 | 5 | VEG | TOMATO | EG | 5 | 5 | |||
12 | TOTAL | 5 | 0 | 5 | |||||
EXPECTED RESULT |
Cell Formulas | ||
---|---|---|
Range | Formula | |
E10:G10,E5:G5 | E5 | =SUM(E2:E4) |
G11,G6:G9,G2:G4 | G2 | =IFERROR(E2-F2,IF(ISNUMBER(E2),E2,F2*-1)) |
E12:G12 | E12 | =SUM(E11) |
Last edited by a moderator: