VBA for grouping lines with same ID

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Hello Gurus,
could you give me any suggestions on how to write VBA script to group lines with same ID(they are different in values) for further scenario breakdowns? A simplified table is shown below. Thanks!

IDValue
A1
B3
B1
C10
C1
C2
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
What kind of result are you expecting?

For ID C, how should this be grouped? Should one column have C and the other column have 10-1-2? Or should it be 10,1,2 or 1012 etc? How should the values be grouped? Or maybe you are hoping to sum these?
 
Upvote 1
to group lines with same ID
Grouping lines can be interpreted a few different ways in Excel so I'm not sure just what you are after

1. They are already "grouped" in that all the "A" values are together, all the "B" values are together etc.

2. Here is another way they can be "grouped"

1705449779112.png


3. A third way using Subtotal

1705450328700.png



This is code to produce #3 above, starting with, for example, the sample data below

VBA Code:
Sub Group_By_SubTotal()
  With Range("A1").CurrentRegion
    .Sort Key1:=.Columns(1), Order1:=xlAscending, Header:=xlYes
    .Subtotal GroupBy:=1, Function:=xlSum, TotalList:=Array(2), Replace:=True, SummaryBelowData:=True
  End With
End Sub

yxz152830.xlsm
AB
1IDValue
2B1
3A1
4C10
5B3
6C1
7C2
Sheet1
 
Upvote 1
Solution
@yxz152830 I don’t believe I solved your question yet.

@Peter_SSs gave you some options as well.

You have not told us how you’d like for these to be grouped.
 
Upvote 0

Forum statistics

Threads
1,215,139
Messages
6,123,263
Members
449,093
Latest member
Vincent Khandagale

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