Making Groups in 'For Each' loop

howiechiang

New Member
Joined
Jan 27, 2014
Messages
12
I am compiling a list of BOMs. It will first list the assembly name, and then list all of it's components in the rows underneath.

The assemblies name and fields pulled with it must be bold. The list of components underneath it must be group together, so i can collapse all the groups and just see the assemblies.

In my code, the "For Each oAssembly In oAssemblies" it pulls assembly p/n, quantity, and description. The "For Each oCell In oQuantity" nested in that loop pulls the information of individual piece parts in that assembly.

Please help.

Code:
Public Sub SortBOM()


Dim oAssembliesRow As Range
Dim oAssemblies As Range
Dim oBOM As Range
Dim oPieceParts As Range
Dim oQuantity As Range
Dim oCell As Range
Dim oAssembly As Range




'Delete current BOM sheet and create a new'
For Each ws In ThisWorkbook.Sheets
    If ws.Name = "BOM" Then
        ws.Delete
    End If
Next
Sheets.Add.Name = "BOM"




'Select cells to check for quantity & location to store that information'
Set oAssembliesRow = ThisWorkbook.Worksheets("Parts Count").Range("D4:CC4")


Set oBOM = Worksheets("BOM").Range("A1")
Set oAssemblies = oAssembliesRow.SpecialCells(xlCellTypeConstants)




'Input the title of fields in oBOM sheet'
oBOM.Value = "Assemblies"
oBOM.Offset(0, 1).Value = "Piece Part"
oBOM.Offset(0, 2).Value = "Revision"
oBOM.Offset(0, 3).Value = "Quantity"
oBOM.Offset(0, 4).Value = "Description"


'Create'
For Each oAssembly In oAssemblies
    Worksheets("Parts Count").Activate
    
    Set oPieceParts = Range(Cells(7, oAssemblies.Column), Cells(Rows.Count, oAssemblies.Column))
    Set oQuantity = oPieceParts.SpecialCells(xlCellTypeConstants)
    oPieceParts.SpecialCells(xlCellTypeConstants).Activate
    
    oBOM.Offset(1, 0).Value = Worksheets("Parts Count").Cells(6, oAssemblies.Column).Value
    oBOM.Offset(1, 3).Value = Worksheets("Parts Count").Cells(4, oAssemblies.Column).Value
    oBOM.Offset(1, 4).Value = Worksheets("Parts Count").Cells(5, oAssemblies.Column).Value
    
    Set oBOM = oBOM.Offset(1, 0)
    Worksheets("BOM").Activate


            For Each oCell In oQuantity
                Set oBOM = oBOM.Offset(1, 0)
                oBOM.Offset(0, 1).Value = Worksheets("Parts Count").Cells(oCell.Row, 2).Value
                oBOM.Offset(0, 2).Value = Worksheets("Parts Count").Cells(oCell.Row, 3).Value
                oBOM.Offset(0, 3).Value = Worksheets("Parts Count").Cells(oCell.Row, oQuantity.Column).Value
                oBOM.Offset(0, 4).Value = Worksheets("Parts Count").Cells(oCell.Row, 1).Value
        
            Next oCell




Next oAssembly


End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello
I'm not sure if I'm understanding the layout of the "Parts Count" sheet. Could you post a partial view of it using Excel Jeanie, or upload it to a hosting site?
 
Upvote 0
There are rows for all components, and each column represents a different assembly. If the assembly contains a piece part, it will have the number used on the assembly in that row. It's sensitive materials so I cannot.
 
Upvote 0
I don't need the actual data, just the layout. To write code we must know the exact location of the items on the sheet.
I'm assuming your current code does not work properly, because I ran it on this sample data:

Parts Count

*ABCDEFGHIJKL
1A1B1C1D1E1F1G1H1I1J1K1L1
2A2B2C2D2E2F2G2H2I2J2K2L2
3A3B3C3D3E3F3G3H3I3J3K3L3
4A4B4C4D4E4F4G4H4I4J4K4L4
5A5B5C5D5E5F5G5H5I5J5K5L5
6A6B6C6D6E6F6G6H6I6J6K6L6
7A7B7C7D7E7F7G7H7I7J7K7L7
8A8B8C8D8E8F8G8H8I8J8K8L8
9A9B9C9D9E9F9G9H9I9J9K9L9
10A10B10C10D10E10F10G10H10I10J10K10L10
11A11B11C11D11E11F11G11H11I11J11K11L11
12A12B12C12D12E12F12G12H12I12J12K12L12
13A13B13C13D13E13F13G13H13I13J13K13L13
14A14B14C14D14E14F14G14H14I14J14K14L14
15A15B15C15D15E15F15G15H15I15J15K15L15
16A16B16C16D16E16F16G16H16I16J16K16L16
17A17B17C17D17E17F17G17H17I17J17K17L17
18A18B18C18D18E18F18G18H18I18J18K18L18
19A19B19C19D19E19F19G19H19I19J19K19L19
20A20B20C20D20E20F20G20H20I20J20K20L20
21A21B21C21D21E21F21G21H21I21J21K21L21
22A22B22C22D22E22F22G22H22I22J22K22L22
23A23B23C23D23E23F23G23H23I23J23K23L23
24A24B24C24D24E24F24G24H24I24J24K24L24
25A25B25C25D25E25F25G25H25I25J25K25L25
26A26B26C26D26E26F26G26H26I26J26K26L26
27A27B27C27D27E27F27G27H27I27J27K27L27
28A28B28C28D28E28F28G28H28I28J28K28L28
29A29B29C29D29E29F29G29H29I29J29K29L29
30A30B30C30D30E30F30G30H30I30J30K30L30
31656667686970717273747576

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

and it returned this range repeated nine times, which seems incorrect:

BOM

*ABCDE
1AssembliesPiece PartRevisionQuantityDescription
2D6**D4D5
3*B7C7D7A7
4*B8C8D8A8
5*B9C9D9A9
6*B10C10D10A10
7*B11C11D11A11
8*B12C12D12A12
9*B13C13D13A13
10*B14C14D14A14
11*B15C15D15A15
12*B16C16D16A16
13*B17C17D17A17
14*B18C18D18A18
15*B19C19D19A19
16*B20C20D20A20
17*B21C21D21A21
18*B22C22D22A22
19*B23C23D23A23
20*B24C24D24A24
21*B25C25D25A25
22*B26C26D26A26
23*B27C27D27A27
24*B28C28D28A28
25*B29C29D29A29
26*B30C30D30A30
27*66676865

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:81px;"><col style="width:73px;"><col style="width:64px;"><col style="width:64px;"><col style="width:82px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,572
Members
448,972
Latest member
Shantanu2024

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