put the values multiple columns into adjacent value based on match another column

leap out

Active Member
Joined
Dec 4, 2020
Messages
288
Office Version
  1. 2016
  2. 2010
Hi Guys
I hope finding way to brings data from sheet report . so should match column B:D for sheet REPORT with column B for sheet STOCK , then should put the data into E:I for each adjacent cell into column B after match column B:D for sheet REPORT and if there are items are existed in sheet REPORT but are not existed in column B for sheet STOCK , then should be added into E: I as shows in result . as to the values in column I should brings from the last column NET . the last column NET (M) is not the last column. I will insert new columns then will change the column NET in location (this means the column M is not range end when brings the values if I insert new column after it ) .

sheet REPORT
(5).xlsx
ABCDEFGHIJKLM
1CLASSIFICATIONGOODSMARKMANFACTUREBUYINGSELLINGNETBUYINGSELLINGNETBUYINGSELLINGNET
2OIL-AS2 MLO-1 10W40 208L TT/LCASSU20020020040360-2358
310W40 208LENIIT30030040020680400201060
45W30 208LQ8EU120120602016060-220
510W40 208LQ8EU100100100-100-
6TOTAL7200720660801300--1638
7OIL-AS210W40 12x1LQ8EU12312310010213-10203
810W40 12x1LCASSU3030600562560051220
910W40 12x1LENIIT120120125245125-370
10OL-115W40 12x1LCASSU**200131131-131-
11TOTAL27302739561512147251461793
12MS-OIL15W30 12x1LQ8EU200200100300100-400
1310W40 4x4LQ8EU300300300--300
14OIL/M 10W40 4x4LCAS TRMSU N3-90230230230--230
1510W40 4x4L MNH/80-TENIIT L66RT123123125125123125125123
165W40 4x4LQ8EU456456456--456
175W40 4x4LCASSU234234234--234
185W40 4x4LENIIT230230202023020-250
1920W50 4x4LQ8EU1231231013310-143
2020W50 4x4LCASSU56056012548-12536
2120W50 4x4LENIIT120120120--120
225W30 4x4LQ8EU303030--30
23TOTAL26060260625515727042551372822
REPORT
Cell Formulas
RangeFormula
M6,E11:M11,E6:J6E6=SUM(E2:E5)
G12:G22,G7:G10,G2:G5G2=E2-F2
M12:M22,M7:M10,M2:M5,J12:J22,J7:J10,J2:J5J2=G2+H2-I2
E23:M23E23=SUM(E12:E22)


sheet STOCK
(5).xlsx
ABCDEFGHI
1ITEMGOODSQTYITEMGOODSMARKMANFACTURENET
2110W40 12x1L Q8 EU200
3210W40 12x1L CAS SU120
4310W40 12x1L ENI IT300
54OL-115W40 12x1L CAS SU**2240
65 MLO-1 10W40 208L TT/L CAS SU150
7610W40 208L ENI IT160
875W30 208L Q8 EU200
98OIL/M 10W40 4x4L CAS TRM SU N3-90120
10
11
12
13
14
15
16
17
18
19
20
STOCK


after

(5).xlsx
ABCDEFGHI
1ITEMGOODSQTYITEMGOODSMARKMANFACTURENET
2110W40 12x1L Q8 EU200110W40 12x1LQ8EU203
3210W40 12x1L CAS SU120210W40 12x1LCASSU1220
4310W40 12x1L ENI IT300310W40 12x1LENIIT370
54OL-115W40 12x1L CAS SU**22404OL-115W40 12x1LCASSU**2-
65 MLO-1 10W40 208L TT/L CAS SU1505 MLO-1 10W40 208L TT/LCASSU358
7610W40 208L ENI IT160610W40 208LENIIT1060
875W30 208L Q8 EU20075W30 208LQ8EU220
98OIL/M 10W40 4x4L CAS TRM SU N3-901208OIL/M 10W40 4x4LCAS TRMSU N3-90230
10910W40 208LQ8EU-
11105W30 12x1LQ8EU400
121110W40 4x4LQ8EU300
131210W40 4x4L MNH/80-TENIIT L66RT123
14135W40 4x4LQ8EU456
15145W40 4x4LCASSU234
16155W40 4x4LENIIT250
171620W50 4x4LQ8EU143
181720W50 4x4LCASSU536
191820W50 4x4LENIIT120
20195W30 4x4LQ8EU30
STOCK
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try VBA solution.
If you are new VBA user:
1) Alt-F11 to open VBA window
2) Insert/ Modules/Module 1
3) paste below code into edit window
4) Hit F5 (or "play" button in menu bar) to run the code
VBA Code:
Option Explicit
Sub TEST()
Dim lr&, lc&, k&, i&, j&, t&, count&, cell As Range, report(), stock(), res1(), res2(1 To 100000, 1 To 5), res3()
With Sheets("REPORT")
    lr = .Cells(Rows.count, "B").End(xlUp).Row
    lc = Evaluate("=LOOKUP(2,1/(REPORT!1:1=""NET""),COLUMN(REPORT!1:1))")
    ReDim report(1 To lr - 1, 1 To 5)
    ReDim res3(1 To lr - 1, 1 To 5)
    For Each cell In .Range("B2:B" & lr)
        If Not IsEmpty(cell.Offset(0, 1)) Then
            k = k + 1
            report(k, 1) = WorksheetFunction.Trim(cell) & " " & cell.Offset(0, 1) & " " & cell.Offset(0, 2)
            report(k, 2) = cell.Offset(0, lc - 2)
            report(k, 3) = cell
            report(k, 4) = cell.Offset(0, 1)
            report(k, 5) = cell.Offset(0, 2)
         End If
    Next
End With
With Sheets("STOCK")
    lr = .Cells(Rows.count, "B").End(xlUp).Row
    ReDim stock(1 To lr - 1, 1 To 1)
    k = 0
    For Each cell In .Range("B2:B" & lr)
        k = k + 1
        stock(k, 1) = WorksheetFunction.Trim(cell)
    Next
ReDim res1(1 To UBound(stock), 1 To 5)
k = 0
For i = 1 To UBound(stock)
    For j = 1 To UBound(report)
        If stock(i, 1) = report(j, 1) Then
            k = k + 1
            res1(k, 1) = k
            res1(k, 2) = report(j, 3)
            res1(k, 3) = report(j, 4)
            res1(k, 4) = report(j, 5)
            res1(k, 5) = report(j, 2)
        End If
    Next
Next
For i = 1 To UBound(report)
    count = 0
    For j = 1 To UBound(stock)
        If report(i, 1) = stock(j, 1) Then
            count = count + 1
        End If
    Next
    If count = 0 And report(i, 1) <> "" Then
        t = t + 1
        k = k + 1
        res2(t, 1) = k
        res2(t, 2) = report(i, 3)
        res2(t, 3) = report(i, 4)
        res2(t, 4) = report(i, 5)
        res2(t, 5) = report(i, 2)
    End If
Next
.Range("E2:I1000000").ClearContents
.Range("E2").Resize(UBound(res1), 5).Value = res1
.Range("E2").Offset(UBound(res1), 0).Resize(UBound(res2), 5).Value = res2
End With
End Sub
 
Upvote 0
great ! this is exactly what I want. but I have question why the code seems slow. it gives running speed 0.940 I thinks this much time with comparison small data if you can make it 0.02 or 0.03 will be great and I truly appreciate for that
 
Upvote 0
great ! this is exactly what I want. but I have question why the code seems slow. it gives running speed 0.940 I thinks this much time with comparison small data if you can make it 0.02 or 0.03 will be great and I truly appreciate for that
With few thousand records of data
"For Each cell..."
working with cell located in the sheet have no issue.
With quite larger data, variable array should be use to read/write from/to sheet
How large is your actual data?
 
Upvote 0
actualy rows =sheet1's rows * sheet2's rows (you can see few double loops in my code)
OK. Will come back tomorrow with array solution.
 
Upvote 0
Try again to see if its perfomance is better

VBA Code:
Option Explicit
Sub TEST()
Dim lr&, lc&, k&, i&, j&, T&, count&, report(), stock(), res1(), res2(1 To 100000, 1 To 5), res3()
Dim rng
With Sheets("REPORT")
    lr = .Cells(Rows.count, "B").End(xlUp).Row
    lc = Evaluate("=LOOKUP(2,1/(REPORT!1:1=""NET""),COLUMN(REPORT!1:1))")
    ReDim report(1 To lr - 1, 1 To 5)
    ReDim res3(1 To lr - 1, 1 To 5)
    rng = .Range("B2", .Cells(lr, lc)).Value
    For i = 1 To lr - 1
        If rng(i, 2) <> "" Then
            k = k + 1
            report(k, 1) = WorksheetFunction.Trim(rng(i, 1)) & " " & rng(i, 2) & " " & rng(i, 3)
            report(k, 2) = rng(i, lc - 1)
            report(k, 3) = rng(i, 1)
            report(k, 4) = rng(i, 2)
            report(k, 5) = rng(i, 3)
         End If
    Next
End With
With Sheets("STOCK")
    lr = .Cells(Rows.count, "B").End(xlUp).Row
    ReDim stock(1 To lr - 1, 1 To 1)
    rng = .Range("B2:B" & lr).Value
    For i = 1 To lr - 1
            stock(i, 1) = WorksheetFunction.Trim(rng(i, 1))
    Next
ReDim res1(1 To UBound(stock), 1 To 5)
k = 0
For i = 1 To UBound(stock)
    For j = 1 To UBound(report)
        If stock(i, 1) = report(j, 1) Then
            k = k + 1
            res1(k, 1) = k
            res1(k, 2) = report(j, 3)
            res1(k, 3) = report(j, 4)
            res1(k, 4) = report(j, 5)
            res1(k, 5) = report(j, 2)
        End If
    Next
Next
For i = 1 To UBound(report)
    count = 0
    For j = 1 To UBound(stock)
        If report(i, 1) = stock(j, 1) Then
            count = count + 1
        End If
    Next
    If count = 0 And report(i, 1) <> "" Then
        T = T + 1
        k = k + 1
        res2(T, 1) = k
        res2(T, 2) = report(i, 3)
        res2(T, 3) = report(i, 4)
        res2(T, 4) = report(i, 5)
        res2(T, 5) = report(i, 2)
    End If
Next
.Range("E2:I1000000").ClearContents
.Range("E2").Resize(UBound(res1), 5).Value = res1
.Range("E2").Offset(UBound(res1), 0).Resize(UBound(res2), 5).Value = res2
End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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