Macro - sorting by a specific word

ApolloID

Well-known Member
Joined
Jun 8, 2010
Messages
769
Hi,

I have the below table and i need a macro to sort by specific words in column "D", words that are part of a group, then to add the name of the group in column B. And after each group to make a total for that group's value and qty.
The table starts from A5.

SupplierGroupCodDescriptionValueQty
supplier 1s 228scissor 13,781.191,614.00
supplier 2t 141tweezer 822,187.251,958.00
supplier 2t 222tweezer 1015,515.334,184.00
supplier 1s 259scissor 898,214.133,435.00
supplier 1s 235cuticle scissor 331,989.64775.00
supplier 2t389oblique tweezer 162,989.121,312.00

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>

This is how i need the table to be:
SupplierGroupCodDescriptionValueQty
supplier 1scissors 228scissor 13,781.191,614.00
supplier 1scissors 259scissor 898,214.133,435.00
supplier 1scissors 235cuticle scissor 331,989.64775.00
scissor total13,984.965,824.00
supplier 2tweezert 141tweezer 822,187.251,958.00
supplier 2tweezert 222tweezer 1015,515.334,184.00
supplier 2tweezert389oblique tweezer 162,989.121,312.00
tweezer total10,691.707,454.00

<colgroup><col><col><col><col><col><col></colgroup><tbody>
</tbody>

I need to make groups inside the macro with products that will be part of that group. The words could be at the begining or as part of the product name.
Also, it would be great to put all unidentified products to an "Unknown" group at the end of all sorted groups.

Can this be done?
Thank you in advance,

Apollo.
 
Hi Peter_SSs, Hiker95,

You are right, i should have specified your names, but i tried both codes and both are ok. :)

And now after the last changes, with the color, it's perfect.
I did tried with a copy, but everything seems ok. I will play a little more just to be sure.

But everything seems perfect!

Thank you both very very much.

Have a wonderful day,
Apollo
 
Upvote 0

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.
Hi Peter_SSs, Hiker95,

You are right, i should have specified your names, but i tried both codes and both are ok. :)

And now after the last changes, with the color, it's perfect.
I did tried with a copy, but everything seems ok. I will play a little more just to be sure.

But everything seems perfect!

Thank you both very very much.

Have a wonderful day,
Apollo
Great news! Thanks for letting us know. :)

As a matter of interest, about how many rows of data do you have?
 
Upvote 0
Hi Peter_SSs,

I have 1142 rows with data for last year and another one for this year with 862 rows.

Thank you again for your help.

Apollo
 
Upvote 0
Hi, Peter_SSs, Hiker95,

I run into a problem. I have some products that have different name from the group. And i need to add this products to a certain group.
The name of the products is the one we have in the sistem and cannot be modified. But it belongs to a certain group. I know you worn me about something like this :)

Can your macros look in sheet 2, and see each product's group? (by code). Something like vlookup?
I will add a group for each reference. I know it's timeconsuming but it's the only way i can think of.

In sheet 2 :
SupplierGroupCodDescription
supplier 1scissors 228scissor 1
supplier 2tweezert 141tweezer 82

<tbody>
</tbody>
i Have the "cod" in sheet2 as well as sheet1.

The rest of the macro to be identical with the one you both provided.

Can this be done?
Thank you again.

Apollo
 
Upvote 0
ApolloID,

But everything seems perfect!

Thank you both very very much.

Thanks for the feedback.

You are very welcome. Glad we could help.


Can your macros look in sheet 2, and see each product's group? (by code). Something like vlookup?

I do not understand - should your latest change in Sheet2, affect the outcome in Sheet1?????

If so, can we have a screenshot of what Sheet1 should look like after a macro (manually formatted by you) for what the new results should look like?
 
Last edited:
Upvote 0
Hi Hiker95,

The result should be identical as before. I just want to be able to select which product goes to Scissor's group, which product goes to BodyCare group.
Now, the macro is naming a group based on a word from the description, right?

Now, it's enough to have in sheet2 only a column with:

Scissor
Tweezer
BodyCare
...and so on.

and based on similar words from description found in sheet1, the macro puts the name of the group.
Now, i would like the macro to look in sheet1, and see the "cod", then to look in sheet2 for that code and return to sheet1 the group's name.


Or, now i realize that it could be easier. In sheet2 i will make a database with group name for all products, and then with vlookup i will bring in sheet1 the group for each product.
Then, i will just need a macro that will only reorganize the data and insert the total with all formulas and color. I think this will be easier.

One question though, can the formulas be kept after i will run the Macro? Now, for example, the percentage is formatted as value.

Thank you kindly for all your support,

Apollo.
 
Upvote 0
ApolloID,

Then, i will just need a macro that will only reorganize the data and insert the total with all formulas and color. I think this will be easier.

One question though, can the formulas be kept after i will run the Macro?

Both versions of my macro produce the majority of the results with formulae. I can adjust those that do not display a formula, to display a formula.


To continue:
1. I will need to see the new screenshot of Sheet2.
2. I will have to see the screenshot of Sheet1 before the macro.
3. And, I will have to see a screenshot of Sheet1 (manually formatted by you) for the results you are now looking for.
 
Upvote 0
See how this version goes.
It assumes that in Sheet2 Group Names are in column B and Cod values in column C.
Any items that cannot be attributed to a group will be grouped as "unknown" as earlier requested.

Rich (BB code):
Sub Make_Groups()
  Dim lr As Long
  Dim sGrpAdr As String, sCodAdr As String
  
  Application.ScreenUpdating = False
  With Sheets("Sheet2")
    sGrpAdr = "'" & .Name & "'!B$1:B$" & .Range("C" & .Rows.Count).End(xlUp).Row
    sCodAdr = "'" & .Name & "'!C$1:C$" & .Range("C" & .Rows.Count).End(xlUp).Row
  End With
  With Sheets("Sheet1")
    lr = .Range("A" & .Rows.Count).End(xlUp).Row
    With .Range("B2:B" & lr)
      .Formula = Replace(Replace("=IFERROR(INDEX(#,MATCH(C2,%,0)),""unknown"")", "#", sGrpAdr), "%", sCodAdr)
      .Value = .Value
    End With
    With .Range("A1").CurrentRegion
      .Sort Key1:=.Cells(2, 2), Order1:=xlAscending, Header:=xlYes, OrderCustom:=1, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
      .Subtotal GroupBy:=2, Function:=xlSum, TotalList:=Array(5, 7), Replace:=True, PageBreaks:=False, SummaryBelowData:=True
    End With
    lr = .Range("E" & .Rows.Count).End(xlUp).Row
    With .Columns("E:G").SpecialCells(xlFormulas).Offset(, 1)
      .FormulaR1C1 = "=RC[-1]/R" & lr & "C[-1]"
      .NumberFormat = "0%"
      .EntireRow.Font.Bold = True
      With Intersect(.EntireRow, .Parent.Columns("A:H"))
        .Font.Bold = True
        .Font.Color = vbBlue
        .Interior.Color = vbYellow
      End With
    End With
  End With
  Application.ScreenUpdating = True
End Sub


I have other columns in Sheet2 but only B & C are used by my code.

Excel Workbook
ABCD
1SupplierGroupCodDescription
2supplier 1scissors 228scissor 1
3supplier 2tweezert 141tweezer 82
4supplier 2tweezert 222tweezer 101
5supplier 1scissors 259scissor 89
6supplier 1scissors 235cuticle scissor 33
7supplier 2tweezert389oblique tweezer 16
8
Sheet2




Excel Workbook
ABCDEFGH
1SupplierGroupCodDescriptionValue%Qty%
2supplier 1s 228scissor 13,781.191,614.00
3supplier 2t 141tweezer 822,187.251,958.00
4supplier 2t 222tweezer 1015,515.334,184.00
5supplier 3xxxxsomething888.88300.00
6supplier 1s 259scissor 898,214.133,435.00
7supplier 2y 456else7.005.00
8supplier 1s 235scissor 331,989.64775
9supplier 2t389tweezer 162,989.121,312.00
Sheet1 (Before)




The sheet below contains formulas in all yellow cells in columns E:H (I just haven't shown them here)

Excel Workbook
ABCDEFGH
1SupplierGroupCodDescriptionValue%Qty%
2supplier 1scissors 228scissor 13,781.191,614.00
3supplier 1scissors 259scissor 898,214.133,435.00
4supplier 1scissors 235scissor 331,989.64775
5scissor Total13,984.9655%582443%
6supplier 2tweezert 141tweezer 822,187.251,958.00
7supplier 2tweezert 222tweezer 1015,515.334,184.00
8supplier 2tweezert389tweezer 162,989.121,312.00
9tweezer Total10,691.7042%7,454.0055%
10supplier 3unknownxxxxsomething888.88300.00
11supplier 2unknowny 456else7.005.00
12unknown Total895.884%305.002%
13Grand Total25,572.54100%13,583.00100%
Sheet1 (After)
 
Upvote 0
Hi Peter_SSs, Hiker95,

Sorry for my late reply. I didn't had access to internet.

Hiker95, this is how the tables should be:

Sheet2

ABCD
1SupplierGroupCodDescription
2supplier 1scissors 228scissor 1

<colgroup><col style="width: 30px;"><col style="width: 75px;"><col style="width: 66px;"><col style="width: 72px;"><col style="width: 138px;"></colgroup><tbody>
</tbody>
Sheet1

ABCDEFGH
1SupplierGroupCodDescriptionValue%Qty%
2supplier 1 s 228scissor 13,781.19 1,614.00

<colgroup><col style="width: 30px;"><col style="width: 99px;"><col style="width: 93px;"><col style="width: 56px;"><col style="width: 98px;"><col style="width: 81px;"><col style="width: 56px;"><col style="width: 80px;"><col style="width: 56px;"></colgroup><tbody>
</tbody>
Sheet1 after the macro

ABCDEFGH
1SupplierGroupCodDescriptionValue%Qty%
2supplier 1scissors 228scissor 13,781.19 1,614.00

<colgroup><col style="width: 30px;"><col style="width: 99px;"><col style="width: 106px;"><col style="width: 56px;"><col style="width: 98px;"><col style="width: 81px;"><col style="width: 58px;"><col style="width: 80px;"><col style="width: 56px;"></colgroup><tbody>
</tbody>

Peter_SSs, thank you for your macro. it's working.
I will test with all products and be back with an answer.

Sorry again to both of you for the late reply, and thanks for your help.

Apollo.
 
Upvote 0
ApolloID,

Based on your latest reply containing the three, two line, screenshots, there is not enough information for me to continue.

It looks like Peter_SSs has a working solution for you.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,687
Members
449,117
Latest member
Aaagu

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