Try to create a new table to add the title to the associated entry for the group, Your help is highly appreicated

zifu886

New Member
Joined
Jul 10, 2019
Messages
13
how to write a formula to add the "Product" field (New Column) to each entry under the same sub-group to create a new table (see below), your help is highly appreciated & thank you very much

Source Table
Product-OrderJANFEBMARSUBTOTAL
Apple 246
ord-1001 2 2
ord-1004 11
ord-1301 33
Oranage4 59
ord-10094 4
ord-1100 55
Kiwi26715
ord-111021 1
ord-1201 2 2
ord-11109 4 4
ord-12010 77
New Table
ProductOrderJANFEBMARSUBTOTAL
Apple 246
Appleord-1001 2 1
Appleord-1004 13
Appleord-1301 34
Oranage 4 59
Oranageord-10094 5
Oranageord-1100 55
Kiwi 26715
Kiwiord-111022 2
Kiwiord-1201 2 2
Kiwiord-11109 4 4
Kiwiord-12010 77

<colgroup><col><col><col><col><col span="2"><col></colgroup><tbody>
</tbody>
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
how to write a formula to add the "Product" field (New Column) to each entry under the same sub-group to create a new table (see below), your help is highly appreciated & thank you very much

Source Table
Product-OrderJANFEBMARSUBTOTAL
ord-130133
Orange459

<tbody>
</tbody>

Some questions:

How to identify if "ord-1301" is an order or a product or how to identify if "Orange" is an order or a product?

Do you need the result on the same sheet or on another sheet?

Can it be with macro?
 
Upvote 0
Dear Dante,

the original table layout is as following

the cell value starting with ord-xxx is an order entry associated to a certain Product (such as Apple, Orange, etc). A2 is the first product. each Product group is shown each month of sales, the sales detailed is listed below Product entry. In original table, A2 is first product sub total breaking by month, next row(s), will be each ord-# associated with A2, until next n row's value of A? starting not <> ord-xxxx, we know this is a new product starting, does this make sense?
Yes, we can use macro, if you can guide me or provide me a macro to achieve this goal, thank you very much

Zi,
 
Upvote 0
HI Dante,
Yes, I would like to create a new table in a new sheet.
thank you once again,
Zi


Try this

Code:
Sub [COLOR=#0000ff]create_table[/COLOR]()
    Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
    Dim order As String, n As Long, lr As Long
    
    Application.ScreenUpdating = False
    Set sh1 = Sheets("Source")
    Set sh2 = Sheets("New")
    sh2.Cells.ClearContents
    sh1.Rows(1).Copy sh2.Rows(1)
    sh2.Columns("B").Insert
    sh2.Range("B1").Value = "Order"
    
    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        If LCase(Left(c, 4)) = LCase("ord-") Then order = c Else order = ""
        n = sh1.Cells(1, Columns.Count).End(xlToLeft).Column - 1
        lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
        sh2.Range("A" & lr).Resize(1, 2).Value = Array(c, order)
        sh2.Range("C" & lr).Resize(1, n).Value = c.Offset(, 1).Resize(1, n).Value
     Next
End Sub

INSERT A MODULE
Press Alt-F11 to open the VBA editor. From the menu select Insert > Module. On the sheet that opens, paste the code previous.
Close the editor (press Alt-Q). From Excel, press Alt-F8 to open the macro selector, and select create_table and press Run.
 
Upvote 0
Hi Dante,

good afternoon,

after run the macro, the output is not expected, what I tried to create is as following table - adding the product name to each order (font in red in column-B).
In this case, the each fruit has a subgroup entry, Column A with fruit name (eg Apple), the row below subgroup entry is the order info associated with the subgroup, which Column-A with left(A,4)="ORD-", each subgroup may have multiple order entries, until next row Column-A (left,4) <> "0RD-", we need this is new subgroup entry, so on and so forth,

I tried to modify the codes myself to create the table needed but failed, Could you please take a look how to modify the code to achieve the goal, thank you very much, I really appreciate your great help,

Sincerely,
Zi

Product-OrderOrderJANFEBMARTotal
Apple 101314
ord-1001ord-1001 << replace order_xxxx with "Apple"2 2
ord-1009ord-1009 << replace order_xxxx with "Apple"8 8
ord-1100ord-1100 << replace order_xxxx with "Apple" 1 1
ord-1120ord-1120 << replace order_xxxx with "Apple" 3
Orange 3 25
ord-1131ord-1131 < replace order_xxxx with "Orange"3 3
ord-1121ord-1121 < replace order_xxxx with "Orange" 33

<colgroup><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
maybe something like this?

Sum of ValueMonth
ProductOrdJANFEBMARGrand Total
Appleord-1001
2​
2​
ord-1004
1​
1​
ord-1301
3​
3​
Apple Total
2
4
6
Kiwiord-11102
1​
1​
ord-11109
4​
4​
ord-1201
2​
2​
ord-12010
7​
7​
Kiwi Total
1
6
7
14
Oranageord-1009
4​
4​
ord-1100
5​
5​
Oranage Total
4
5
9
Grand Total
5
8
16
29
 
Upvote 0
Hi Sandy666,

good morning, thanks much for your reply. that is perfect table above, while I need add the product name to each order entry which associated with. Something like below

New-Table

Sum of ValueMonth
ProductOrdJANFEBMARGrand Total
Apple Total
2
4
6​
Appleord-1001
1​
1​
Appleord-1004
3​
3​
Appleord-13012
2​
Kiwi Total
1
67
14
Kiwiord-11109
4​
4​
Kiwiord-1201
2​
2​
Kiwiord-12010
7​
7​
Kiwiord-1008
1​

Oranage total
4
5
9
orangeord-1100
5​
5​
Oranageord-122044

<tbody>
</tbody>




Source Table
Product-OrderJANFEBMARSUBTOTAL
Apple246
ord-100122
ord-100411
ord-130133
Oranage459
ord-100944
ord-110055
Kiwi26715
ord-1110211
ord-120122
ord-1110944
ord-1201077

<tbody>
</tbody>
......data truncated ............
 
Upvote 0
like this ?

sourceresult
Product-OrderJANFEBMARSUBTOTALSum of ValueMonth
Apple
2​
4​
6​
ProductOrdJANFEBMARGrand Total
ord-1001
2​
2​
Apple
2
4
6
ord-1004
1​
1​
Appleord-1001
2​
2​
ord-1301
3​
3​
Appleord-1004
1​
1​
Oranage
4​
5​
9​
Appleord-1301
3​
3​
ord-1009
4​
4​
Kiwi
1
6
7
14
ord-1100
5​
5​
Kiwiord-11102
1​
1​
Kiwi
2​
6​
7​
15​
Kiwiord-11109
4​
4​
ord-11102
1​
1​
Kiwiord-1201
2​
2​
ord-1201
2​
2​
Kiwiord-12010
7​
7​
ord-11109
4​
4​
Oranage
4
5
9
ord-12010
7​
7​
Oranageord-1009
4​
4​
Oranageord-1100
5​
5​

you'll need Power Query (Get&Transform) with this M-code:

Code:
[SIZE=1]// Table1
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Type = Table.TransformColumnTypes(Source,{{"Product-Order", type text}, {"JAN", Int64.Type}, {"FEB", Int64.Type}, {"MAR", Int64.Type}, {"SUBTOTAL", Int64.Type}}),
    Order = Table.AddColumn(Type, "Ord", each if Text.Contains([#"Product-Order"], "ord-") then [#"Product-Order"] else null),
    Product = Table.AddColumn(Order, "Product", each if not Text.Contains([#"Product-Order"], "ord-") then [#"Product-Order"] else null),
    FillProd = Table.FillDown(Product,{"Product"}),
    FilterOrd = Table.SelectRows(FillProd, each ([Ord] <> null)),
    ROC = Table.SelectColumns(FilterOrd,{"Product", "Ord", "JAN", "FEB", "MAR"}),
    Unpivot = Table.UnpivotOtherColumns(ROC, {"Product", "Ord"}, "Attribute", "Value"),
    Rename = Table.RenameColumns(Unpivot,{{"Attribute", "Month"}})
in
    Rename[/SIZE]

then you can create PivotTable from Query-Table
 
Last edited:
Upvote 0
Please try this

Code:
Sub create_table()
    Dim sh1 As Worksheet, sh2 As Worksheet, c As Range, p As String
    Dim order As String, n As Long, lr As Long
    
    Application.ScreenUpdating = False
    Set sh1 = Sheets("Source")
    Set sh2 = Sheets("New")
    sh2.Cells.ClearContents
    sh1.Rows(1).Copy sh2.Rows(1)
    sh2.Columns("B").Insert
    sh2.Range("B1").Value = "Order"


    For Each c In sh1.Range("A2", sh1.Range("A" & Rows.Count).End(xlUp))
        If LCase(Left(c, 4)) = LCase("ord-") Then
            order = c
        Else
            order = ""
            p = c.Value
        End If
        n = sh1.Cells(1, Columns.Count).End(xlToLeft).Column - 1
        lr = sh2.Range("A" & Rows.Count).End(xlUp).Row + 1
        sh2.Range("A" & lr).Resize(1, 2).Value = Array(p, order)
        sh2.Range("C" & lr).Resize(1, n).Value = c.Offset(, 1).Resize(1, n).Value
     Next
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,402
Messages
6,119,304
Members
448,886
Latest member
GBCTeacher

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