Bill of materials - consolidated sum

sekar

New Member
Joined
Feb 2, 2009
Messages
33
Office Version
  1. 2010
Platform
  1. Windows
Hi.,

The below code is from one of the thread from the forum.

VBA Code:
Sub MG03Nov36()
Dim Rng As Range, Dn As Range, n As Long, txt As String, Ac As Long
Set Rng = Range(Range("A1"), Range("A" & Rows.Count).End(xlUp))
ReDim ray(1 To Rng.Count, 1 To 6)
With CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
For Each Dn In Rng
    txt = Dn.Offset(, 2).Value & "," & Dn.Offset(, 3).Value & "," & _
        Dn.Offset(, 4).Value & Dn.Offset(, 5).Value
    If Not .Exists(txt) Then
        n = n + 1
        For Ac = 1 To 6
            ray(n, Ac) = Format(Dn.Offset(, Ac - 1), "@")
        Next Ac
        .Add txt, n
    Else
        ray(.Item(txt), 2) = ray(.Item(txt), 2) + Dn.Offset(, 1)
    End If
Next
End With
With Sheets("Sheet2").Range("A1").Resize(n, 6)
.Value = ray
 .Borders.Weight = 2
  .Columns.AutoFit
End With
End Sub

the output is as follows

Book1.xlsm
ABCDEF
1ITEMQTYWIDTHLENGTHTHICKMATL
2124234213/16Solid Ash
32226.37532.87504-03-22Oak Ply
43216.7532.87504-03-22Oak Ply
542332.37504-03-22Solid Ash
6522.7532.37504-03-22Solid Ash
7621.532.37504-03-22Birch Ply
8741.7532.37504-03-22Birch Ply
982429.51.25Hardwood
109241.519.504-03-22Birch Ply
1110214.529.37502-01-22CDX Plywood
122226.37546.87504-03-22Oak Ply
133216.7546.87504-03-22Oak Ply
1442346.37504-03-22Solid Ash
15522.7546.37504-03-22Solid Ash
16621.546.37504-03-22Birch Ply
17741.7546.37504-03-22Birch Ply
1810214.543.37502-01-22CDX Plywood
192426.37552.87504-03-22Oak Ply
203416.7552.87504-03-22Oak Ply
2144352.37504-03-22Solid Ash
22542.7552.37504-03-22Solid Ash
23641.552.37504-03-22Birch Ply
24781.7552.37504-03-22Birch Ply
2510414.549.37502-01-22CDX Plywood
262426.37564.87504-03-22Oak Ply
273416.7564.87504-03-22Oak Ply
2844364.37504-03-22Solid Ash
29542.7564.37504-03-22Solid Ash
30641.564.37504-03-22Birch Ply
31781.7564.37504-03-22Birch Ply
3210414.561.37502-01-22CDX Plywood
Sheet2



but i want consolidated sum of the materials ( 10 materials, no duplicates, with proper sum with respect to width, length and thick) with respect the quantities in the sheet 2. Presently the code is not giving desired output. It gives sum of qty correctly for the following higlighted cell.

Book1.xlsm
ABCDEF
1ITEMQTYWIDTHLENGTHTHICKMATL
2124234213/16Solid Ash
32226.37532.87504-03-22Oak Ply
43216.7532.87504-03-22Oak Ply
542332.37504-03-22Solid Ash
6522.7532.37504-03-22Solid Ash
7621.532.37504-03-22Birch Ply
8741.7532.37504-03-22Birch Ply
982429.51.25Hardwood
109241.519.504-03-22Birch Ply
1110214.529.37502-01-22CDX Plywood
Sheet2
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Is the sheet2 output? We need to see the raw data. Could you attach it?
 
Upvote 0
Is the sheet2 output? We need to see the raw data. Could you attach it?
Hi bebo.,

I attached the raw data input sheet here. Missed to attached earlier. Sorry!.

Yes Sheet2 is the output Sheet, but the output was not correct, as there was additional data as like as input sheet, as well as the sum of qty was not outputted correctly, apart from few as mentioned earlier.

Book2
ABCDEF
1ITEMQTYWIDTHLENGTHTHICKMATL
214234213/16Solid Ash
32226 3/832 7/83/4Oak Ply
43216 3/432 7/83/4Oak Ply
542332 3/83/4Solid Ash
6522 3/432 3/83/4Solid Ash
7621 1/232 3/83/4Birch Ply
8741 3/432 3/83/4Birch Ply
98429 1/21 1/4Hardwood
10941 1/219 1/23/4Birch Ply
1110214 1/229 3/81/2CDX Plywood
1214234213/16Solid Ash
132226 3/846 7/83/4Oak Ply
143216 3/446 7/83/4Oak Ply
1542346 3/83/4Solid Ash
16522 3/446 3/83/4Solid Ash
17621 1/246 3/83/4Birch Ply
18741 3/446 3/83/4Birch Ply
198429 1/21 1/4Hardwood
20941 1/219 1/23/4Birch Ply
2110214 1/243 3/81/2CDX Plywood
2218234213/16Solid Ash
232426 3/852 7/83/4Oak Ply
243416 3/452 7/83/4Oak Ply
2544352 3/83/4Solid Ash
26542 3/452 3/83/4Solid Ash
27641 1/252 3/83/4Birch Ply
28781 3/452 3/83/4Birch Ply
298829 1/21 1/4Hardwood
30981 1/219 1/23/4Birch Ply
3110414 1/249 3/81/2CDX Plywood
3218234213/16Solid Ash
332426 3/864 7/83/4Oak Ply
343416 3/464 7/83/4Oak Ply
3544364 3/83/4Solid Ash
36542 3/464 3/83/4Solid Ash
37641 1/264 3/83/4Birch Ply
38781 3/464 3/83/4Birch Ply
398829 1/21 1/4Hardwood
40981 1/219 1/23/4Birch Ply
4110414 1/261 3/81/2CDX Plywood
Sheet1
 
Upvote 0
How about:
VBA Code:
Sub Test()
Dim lr&, i&, k&, rng, arr(1 To 1000000, 1 To 6), id
Dim dic As Object, key
Set dic = CreateObject("scripting.dictionary")
Sheets("Sheet1").Activate
lr = Cells(Rows.Count, "F").End(xlUp).Row
rng = Evaluate("C2:C" & lr & "& ""|"" & D2:D" & lr & "& ""|"" & E2:E" & lr & " & ""|"" & F2:F" & lr & " & ""@"" & B2:B" & lr)
For i = 1 To UBound(rng)
    id = Split(rng(i, 1), "@")
    If Not dic.exists(id(0)) Then
        dic.Add id(0), id(1)
    Else
        dic(id(0)) = dic(id(0)) + CLng(id(1))
    End If
Next
For Each key In dic.keys
    k = k + 1
    arr(k, 1) = k: arr(k, 2) = dic(key)
    arr(k, 3) = Split(key, "|")(0): arr(k, 4) = Split(key, "|")(1)
    arr(k, 5) = Split(key, "|")(2): arr(k, 6) = Split(key, "|")(3)
Next
Range("T2").Resize(dic.Count, 1).Value = WorksheetFunction.Transpose(dic.keys)
Range("u2").Resize(dic.Count, 1).Value = WorksheetFunction.Transpose(dic.items)
Sheets("Sheet2").Activate
Range("A1:F1000000").Clear
Range("A1:F1").Value = Sheets("Sheet1").Range("A1:F1").Value
With Range("A2").Resize(k, 6)
    .Value = arr
    .Borders.Weight = 2
    .Columns.AutoFit
End With
End Sub
 
Upvote 0
Hi beboo.,


1. The output doesn't summed for Qty of unique values (width, length, thickness and material).
2. The output was not generated in the sheet 2,it overwritten the existing values.
3. New Unwanted values generated in the sheet1,

1662363540816.png
 
Upvote 0
Column T & U is for testing. I deleted it already.
With input data in Sheet1, Output in sheet2:

VBA Code:
Sub Test()
Dim lr&, i&, k&, rng, arr(1 To 1000000, 1 To 6), id
Dim dic As Object, key
Set dic = CreateObject("scripting.dictionary")
Sheets("Sheet1").Activate
lr = Cells(Rows.Count, "F").End(xlUp).Row
rng = Evaluate("C2:C" & lr & "& ""|"" & D2:D" & lr & "& ""|"" & E2:E" & lr & " & ""|"" & F2:F" & lr & " & ""@"" & B2:B" & lr)
For i = 1 To UBound(rng)
    id = Split(rng(i, 1), "@")
    If Not dic.exists(id(0)) Then
        dic.Add id(0), id(1)
    Else
        dic(id(0)) = dic(id(0)) + CLng(id(1))
    End If
Next
For Each key In dic.keys
    k = k + 1
    arr(k, 1) = k: arr(k, 2) = dic(key)
    arr(k, 3) = Split(key, "|")(0): arr(k, 4) = Split(key, "|")(1)
    arr(k, 5) = Split(key, "|")(2): arr(k, 6) = Split(key, "|")(3)
Next
Sheets("Sheet2").Activate
Range("A1:F1000000").Clear
Range("A1:F1").Value = Sheets("Sheet1").Range("A1:F1").Value
With Range("A2").Resize(k, 6)
    .Value = arr
    .Borders.Weight = 2
    .Columns.AutoFit
End With
End Sub
 
Upvote 0
Solution
Thanks bebo.,

You solved the problem, Thanks a lot.
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

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