could extract items based on first & last two items and merge values

abdo meghari

Well-known Member
Joined
Aug 3, 2021
Messages
612
Office Version
  1. 2019
Hi experts
I have two sheets(DATA,REP1) each sheet could be 7800 rows .
every sheet contains items in column B and values into columns C,D should match between two sheets based on column B if the ftwo first items and the two last items are the same thing between two sheets thien should fill the whole items for sheet DATA in columnB in sheet MASTER, and the the whole items for sheet REP1 in column C in sheet MASTER( put each matched item into adjacent cells , and should merge values for column D,E and subtract thae values column D from E as show in column F in sheet MASTER based on adjacent cells in column B,C for each item) . if there is item in sheet DATA and is not existed in sheet REP1, then it considers new item and should highlight by red and show by red in sheet MASTER , also if there is item in sheet REP1 and is not existed in sheet DATA , then it considers new item and should highlight by red and show by red in sheet MASTER
and will change and add data every time in sheets DATA & REP should also updtate automatically by replace data(it meansto clear data in sheet MASTER before bring the data)
let's take example
sheet DATA sheet REP
BS 1200R20 18PR G580 JAP BS 1200R20 G580 JAP
as you see the first two items (BS,1200R20) and the last two items (G580,JAP) are the same thing , then should add in column B,C next to each other of them as in row two and merge the values . last thing if any new item whether in sheet DATA or REP1
contains zero values for two columns C,D together then should n't show in sheet MASTER at all. always when match the item usually contains four or five or six items .....
sometimes can contain three items then can't match based on two first & two last items ,then will be new item directly .

TIRES (1).xlsm
ABCD
1ITEMBRANDQTYBALANCE
21BS 1200R20 18PR G580 JAP1,147.00791.00
32BS 13R22.5 18PR R187 JAP60.00-
43BS 315/80R22.5 18PR R184 JAP98.00200.00
54BS 315/80R22.5 G580 JAP20.00-
65BS 1400R20 VSJ TCF JAP141.00-
76BS 1200R24 TCF 18PR G580 JAP194.00100.00
87BS 385/65R22.5 R164 JAP70.00-
98BS 385/65R22.5 R164 THI200.00100.00
109BS 1200R24 22PR G582 JAP100.00100.00
1110BS 265/70R16 D840 THI40.0050.00
1211BS 205R16C D840 THI40.0050.00
1312BS 195/65R15 EP150 THI50.00-
1413BS 205/70R15C R623 THI200.00360.00
1514BS 215/70R15C R623 THI100.00-
1615BS 175/70R13 EP150 THI20.00-
1716BS 235/55R17 T005 THI40.00-
1819BS 275/70R16 H005 THI150.00-
1920BS 255/70R15C D840 THI200.00-
2021BS 1200R20 G580 THI-137.00
2122BS 315/80R22.5 G582 THI-50.00
2223BS 175/70R14 EP150 THI-50.00
2324BS 215/50R17 EP300 THI-40.00
2425175/65R14 B250 THI-150.00
2526BS 245/70R16 D684 THI-25.00
2627BS 225/55R17 EP300 THI-20.00
2728BS 205/65R15 T005 THI-93.00
2829BS 205/55R16 T005 THI-50.00
2930BS 195R15C 623 THI-162.00
3031BS 255/70R15C D840 THI-150.00
3132BS 195/55R16 EP300 THI-30.00
3233BS 195R14C R623 THI-16.00
3334BS 205/60R16 T005 THI-150.00
3435BS 255/70R16 H005 THI-30.00
3536BS 215/45R17 EA03 THI-50.00
3637BS 1200R20 R187 TCF JAP-89.00
3738BS 325/95R24 G582 JAP-50.00
3839FS 215/60R16 99V XL IT-329.00
3940BS 700R16 12PR R230 JAP--
4041BS 215/70R15C R623 THI--
4142BS 1400R20VSJ TCF JAP--
4243BS 1400R20 TCF R180 JAP--
4344BS 1400R20 TCF R180BZ JAP--
4445BS 185/65R15 B250 JAP--
4546BS 195R15C 613V JAP--
4647BS 205/65R16 EP300 IND--
4748BS 205/70R15C R623 THI--
4849BS 215/55R16 EP300 IND--
4950BS 215/55R17 T005 JAP--
5051BS 215/70R16 D697 IND--
5152BS 225/45R17 050A JAP--
5253BS 225/50R17 EP300 THI--
5354BS 225/55R16 EP300 IND--
5455BS 225/60R16 EP300 IND--
5556BS 225/70R15C R623 JAP--
5657BS 235/45R17 EA03 THI--
5758BS 235/65R17 D697 IND--
5859BS 235/85R16 D697 IND--
5960BS 245/40R18PR 050A JAP--
6061BS 245/65R17 D689 JAP--
6162BS 245/70R17 684A JAP--
6263BS 255/70R16 H005 THI--
6364BS 255/70R15C D840 THI--
6465BS 265/65R17 D840 JAP--
6566BS265/70R15 D697 IND--
6667BS 275/65R18 AL01 JAP--
6768BS 425/65R22.5 R164 JAP--
6869BS 445/65R22.5 R164 JAP--
6970BS 750R16 R230 TCF JAP--
7071BS 225/55R17 EP300 VIT--
7172BS 185/70R14 B250 THI--
7273BS 315/80R22.5-18PR G582 THI--
7374BS 185/65R14 EP150 IND--
7475BS 185/70R13 EP150 IND--
7576BS 215/65R15 T005 IND--
7677BS 265/70R15 D697 IND--
7778BS 315/80R22.5 R184 THI--
7879DT 385/65R22.5 DT40 THI--
7980BS 195R14C R660 TR--
8081BS 235/65R16C R660 TR--
8182BS 195R15C R660 TR--
8283BS 205/70R15C R660 TR--
8384BS 215/70R15C R660 TR11.00-
DATA



TIRES (1).xlsm
ABCD
1ITEMBRANDQTYBALANCE
21BS 215/70R15C R623 THI100.00-
33BS 315/80R22.5 R184 JAP98.00200.00
42BS 13R22.5 R187 JAP60.00-
54BS 205R16C D840 THI40.0050.00
65BS 195/65R15 EP150 THI50.00-
76BS 205/70R15C R623 THI200.00360.00
87BS 385/65R22.5 R164 JAP70.00-
98BS 385/65R22.5 18PR TCF R164 THI62.00100.00
109BS 1200R24 G582 JAP100.00100.00
1110BS 315/80R22.5 G580 JAP20.00-
1211BS 1400R20 VSJ TCF JAP10.00-
1312BS 1200R24 G580 JAP10.0010.00
1413BS 1200R20 G580 JAP200.0010.00
1514BS 265/70R16 D840 THI40.0050.00
1615BS 175/70R13 EP150 THI20.00-
1716BS 235/55R17 T005 THI40.00-
1817BS 225/70R16 H005 THI150.00-
1918BS 255/70R16 H005 THI95.00-
2019BS 275/70R16 H005 THI150.00-
2120BS 255/70R15C D840 THI200.00-
2220BS 315/80R22.5 G582 THI-50.00
2320BS 175/70R14 EP150 THI-50.00
2420BS 215/50R17 EP300 THI-40.00
2520175/65R14 B250 THI-150.00
2620BS 245/70R16 D684 THI-25.00
2720BS 225/55R17 EP300 THI-20.00
2820BS 205/65R15 T005 THI-93.00
2920BS 205/55R16 T005 THI-50.00
3020BS 195R15C R623 THI-162.00
3120BS 255/70R15C D840 THI-150.00
3220BS 195/55R16 EP300 THI-30.00
3320BS 195R14C R623 THI-16.00
3420BS 205/60R16 T005 THI-150.00
3520BS 255/70R16 H005 THI-30.00
3620BS 215/45 R17 EA03 THI-50.00
3720BS 1200R20 R187 TCF JAP-89.00
3820BS 325/95R24 G582 JAP-50.00
3920FS 215/60R16 99V XL IT-329.00
4020BS 700R16 12PR R230 JAP--
4120BS 215/70R15C R623 THI--
4220BS 1400R20VSJ TCF JAP--
4320BS 1400R20 TCF R180 JAP--
4420BS 1400R20 TCF R180BZ JAP--
4520BS 185/65R15 B250 JAP--
4620BS 195R15C 613V JAP--
4720BS 205/65R16 EP300 IND--
4820BS 205/70R15C R623 THI--
4920BS 215/55R16 EP300 IND--
5020BS 215/55R17 T005 JAP--
5120BS 215/70R16 D697 IND--
5220BS 225/45R17 050A JAP--
5320BS 225/50R17 EP300 THI--
5420BS 225/70R15C R623 JAP--
5520BS 235/85R16 D697 IND--
5620BS 245/40R18PR 050A JAP--
5720BS 245/65R17 D689 JAP--
5820BS 245/70R17 684A JAP--
5920BS 255/70R16 H005 THI--
6020BS 255/70R15C D840 THI--
6120BS 265/65R17 D840 JAP--
6220BS265/70R15 D697 IND--
6320BS 275/65R18 AL01 JAP--
6420BS 425/65R22.5 R164 JAP--
6520BS 445/65R22.5 R164 JAP--
6620BS 750R16 R230 TCF JAP--
6720BS 1200R20 18PR G580 THI--
6820BS 225/55R17 EP300 VIT--
6920BS 185/70R14 B250 THI--
7020BS 315/80R22.5-18PR G582 TCF THI--
7120BS 185/65R14 EP150 IND--
7220BS 185/70R13 EP150 IND--
7320BS 215/65R15 T005 IND--
7420BS 265/70R15 D697 IND--
7520BS 315/80R22.5 R184 THI--
7620DT 385/65R22.5 DT40 18PR THI--
7720BS 195R14C R660 TR--
7820BS 235/65R16C R660 TR--
7920BS 235/65R16C R660 IT11.00-
8020BS 195R15C R660 TR--
8120BS 205/70R15C R660 TR--
8220BS 215/70R15C R660 IT-11.00
REP1





result

TIRES (1).xlsm
ABCDEF
1ITEMLIST1LIST2INPUTOUTPUTBALANCE
21BS 1200R20 18PR G580 JAPBS 1200R20 G580 JAP1,347.00801.00546.00
32BS 13R22.5 18PR R187 JAPBS 13R22.5 R187 JAP120.000.00120.00
43BS 315/80R22.5 18PR R184 JAPBS 315/80R22.5 R184 JAP196.00400.00-204.00
54BS 315/80R22.5 G580 JAPBS 315/80R22.5 G580 JAP40.0020.0020.00
65BS 1400R20VSJ TCF JAPBS 1400R20 VSJ TCF JAP10.000.0010.00
76BS 1200R24 TCF 18PR G580 JAPBS 1200R24 G580 JAP204.00110.0094.00
87BS 385/65R22.5 R164 JAPBS 385/65R22.5 R164 JAP140.000.00140.00
98BS 385/65R22.5 R164 THIBS 385/65R22.5 18PR TCF R164 THI362.00200.00162.00
109BS 1200R24 22PR G582 JAPBS 1200R24 G582 JAP200.00200.000.00
1110BS 215/70R15C R660 TR-11.000.0011.00
1211-BS 215/70R15C R660 IT0.0011.00-11.00
MASTER
Cell Formulas
RangeFormula
F2:F12F2=D2-E2


I hope to covered all of detailes without any confusing .
thanks
 
Last edited:

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
What to do with brand like this (only 3 spaces):
"175/65R14 B250 THI"
in DATA cell B24 and in REP1 cell B25?
Is it actually to be, or does it needed to change?
 
Upvote 0
Just give it a first shot:
VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, j&, k&, t&, u1&, pos1&, pos2&, rng, dic As Object, key, st
Dim arr(), arr2(), arr3(), res(), res1(), res2()
Set dic = CreateObject("Scripting.Dictionary")
With Sheets("DATA")
    lr = .Cells(Rows.Count, "B").End(xlUp).Row
    rng = .Range("B2:D" & lr).Value2
End With
    For i = 1 To lr - 1
        If rng(i, 2) + rng(i, 3) > 0 Then
            If Not dic.exists(Trim(rng(i, 1))) Then
                dic.Add Trim(rng(i, 1)), rng(i, 2) & "|" & rng(i, 3)
            Else
                st = Split(dic(Trim(rng(i, 1))), "|")
                st(0) = st(0) + rng(i, 2): st(1) = st(1) + rng(i, 3)
                dic(Trim(rng(i, 1))) = st(0) & "|" & st(1)
            End If
        End If
    Next
    ReDim arr(1 To dic.Count, 1 To 3)
    For Each key In dic.keys
        k = k + 1
        arr(k, 1) = key: arr(k, 2) = Split(dic(key), "|")(0): arr(k, 3) = Split(dic(key), "|")(1)
    Next
    u1 = dic.Count
    dic.RemoveAll
With Sheets("REP1")
    lr = .Cells(Rows.Count, "B").End(xlUp).Row
    rng = .Range("B2:D" & lr).Value2
End With
    For i = 1 To lr - 1
        If rng(i, 2) + rng(i, 3) > 0 Then
            If Not dic.exists(Trim(rng(i, 1))) Then
                dic.Add Trim(rng(i, 1)), rng(i, 2) & "|" & rng(i, 3)
            Else
                st = Split(dic(Trim(rng(i, 1))), "|")
                st(0) = st(0) + rng(i, 2): st(1) = st(1) + rng(i, 3)
                dic(Trim(rng(i, 1))) = st(0) & "|" & st(1)
            End If
        End If
    Next
    k = 0
    ReDim arr2(1 To dic.Count, 1 To 5)
    For Each key In dic.keys
        k = k + 1
        pos1 = InStr(InStr(1, key, " ") + 1, key, " ") - 1: pos2 = Len(key) - InStrRev(key, " ", InStrRev(key, " ") - 1)
        arr2(k, 1) = Left(key, pos1): arr2(k, 2) = Right(key, pos2)
        arr2(k, 3) = Split(dic(key), "|")(0): arr2(k, 4) = Split(dic(key), "|")(1): arr2(k, 5) = "x"
    Next
    k = 0: ReDim arr3(1 To u1, 1 To 6)
    For i = 1 To u1
        k = k + 1
        arr3(k, 1) = k: arr3(k, 2) = arr(i, 1): arr3(k, 4) = CLng(arr(i, 2)): arr3(k, 5) = CLng(arr(i, 3))
        arr3(k, 6) = arr3(k, 4) - arr3(k, 5)
        For j = 1 To UBound(arr2)
            If arr(i, 1) Like arr2(j, 1) & "*" & arr2(j, 2) Then
                arr2(j, 5) = ""
                arr3(k, 3) = arr2(j, 1) & " " & arr2(j, 2): arr3(k, 4) = CLng(arr(i, 2)) + CLng(arr2(j, 3))
                arr3(k, 5) = CLng(arr(i, 3)) + CLng(arr2(j, 4)): arr3(k, 6) = arr3(k, 4) - arr3(k, 5)
            End If
        Next
    Next
    ReDim res(1 To u1 + dic.Count, 1 To 6)
    ReDim res1(1 To u1, 1 To 6)
    ReDim res2(1 To dic.Count, 1 To 6)
    j = 0: k = 0
    For i = 1 To UBound(arr3)
        If arr3(i, 3) = "" Then
            j = j + 1
            For k = 1 To 6
                res1(j, k) = arr3(i, k)
            Next
        Else
            t = t + 1
            For k = 1 To 6
                res(t, k) = arr3(i, k)
            Next
        End If
    Next
    For i = 1 To UBound(arr2)
        If arr2(i, 5) = "x" Then
            t = t + 1
            res(t, 3) = arr2(i, 1) & " " & arr2(i, 2)
            res(t, 4) = arr2(i, 3): res(t, 5) = arr2(i, 4)
            res(t, 6) = res(t, 4) - res(t, 5)
        End If
    Next
    For i = 1 To UBound(res1)
        For k = 2 To 6
            res(t + i, k) = res1(i, k)
        Next
    Next
With Sheets("MASTER")
    .Range("A2").Resize(t + i, 6).Value = res
    .Range("A2").CurrentRegion.Borders.LineStyle = xlContinuous
End With
End Sub
 
Upvote 0
Solution
thanks for following my thread .
Is it actually to be, or does it needed to change?
actually this case should not be at all ,but I give you this case somtimes happen error when enter data , then should be a new item (you can't match based on first and last two items , then should ignore match and it will be new items, is it possible? if not I will correct as the others .
I tested and gives mismatch in this line
VBA Code:
If rng(i, 2) + rng(i, 3) > 0 Then
 
Upvote 0
see the picture please
1.PNG
 
Upvote 0
In debug mode, you can point to the letter "i" in line "For i=1 to lr-1" to see instant value of "i"
Also point to rng(i,2) to see instant value of rng(i,2)
We need to detect wich row of array "rng" get error.
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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