Sort list in table then seperate

nparsons75

Well-known Member
Joined
Sep 23, 2013
Messages
1,256
Office Version
  1. 2016
Hi, is it possible, with a macro, to sort a list in a table and then seperate the list by data in one column.

For example,

[TABLE="width: 500"]
<tbody>[TR]
[TD]MACHINE[/TD]
[TD]ITEM[/TD]
[TD]QTY[/TD]
[TD]DATE[/TD]
[/TR]
[TR]
[TD]001[/TD]
[TD]CAR[/TD]
[TD]5[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]002[/TD]
[TD]BUS
[/TD]
[TD]1[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]009[/TD]
[TD]PLANE[/TD]
[TD]2[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]004[/TD]
[TD]BOAT[/TD]
[TD]5[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]001[/TD]
[TD]SHIP[/TD]
[TD]7[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]002[/TD]
[TD]BIKE[/TD]
[TD]3[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]009[/TD]
[TD]CYCLE[/TD]
[TD]1[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]004[/TD]
[TD]PLANE[/TD]
[TD]1[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]006[/TD]
[TD]BIKE[/TD]
[TD]9[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]004[/TD]
[TD]SHIP[/TD]
[TD]3[/TD]
[TD]16/7/17[/TD]
[/TR]
[TR]
[TD]006[/TD]
[TD]CAR[/TD]
[TD]4[/TD]
[TD]16/7/17[/TD]
[/TR]
</tbody>[/TABLE]


Taking the data above I would like a button to run a macro that will sort the data by column A and then seperate the list by a dividing row between groups. See example below.

In between each of the grouped items I would need a row that has a colour fill to seperate them visually. Would this even be possible? Thanks in advance.

[TABLE="width: 320"]
<colgroup><col width="80" style="width:60pt" span="4"> </colgroup><tbody>[TR]
[TD="class: xl67, width: 80"][TABLE="width: 320"]
<colgroup><col width="80" style="width:60pt" span="4"> </colgroup><tbody>[TR]
[TD="class: xl67, width: 80"]1[/TD]
[TD="class: xl67, width: 80"]CAR[/TD]
[TD="class: xl67, width: 80"]5[/TD]
[TD="class: xl67, width: 80"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl67"]1[/TD]
[TD="class: xl67"]SHIP[/TD]
[TD="class: xl67"]7[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[/TR]
[TR]
[TD="class: xl67"]2[/TD]
[TD="class: xl67"]BUS[/TD]
[TD="class: xl67"]1[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl67"]2[/TD]
[TD="class: xl67"]BIKE[/TD]
[TD="class: xl67"]3[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[/TR]
[TR]
[TD="class: xl67"]4[/TD]
[TD="class: xl67"]BOAT[/TD]
[TD="class: xl67"]5[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl67"]4[/TD]
[TD="class: xl67"]PLANE[/TD]
[TD="class: xl67"]1[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl67"]4[/TD]
[TD="class: xl67"]SHIP[/TD]
[TD="class: xl67"]3[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[/TR]
[TR]
[TD="class: xl67"]6[/TD]
[TD="class: xl67"]BIKE[/TD]
[TD="class: xl67"]9[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl67"]6[/TD]
[TD="class: xl67"]CAR[/TD]
[TD="class: xl67"]4[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[TD="class: xl81"][/TD]
[/TR]
[TR]
[TD="class: xl67"]9[/TD]
[TD="class: xl67"]PLANE[/TD]
[TD="class: xl67"]2[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
[TR]
[TD="class: xl67"]9[/TD]
[TD="class: xl67"]CYCLE[/TD]
[TD="class: xl67"]1[/TD]
[TD="class: xl67"]16/07/2017[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi, is it possible, with a macro, to sort a list in a table and then seperate the list by data in one column.
Try this:
Code:
Sub a1014729a()
Dim rng As Range
Dim i As Long, ra As Long

ra = Range("A:A").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).row
Set rng = Range(Cells(1, "A"), Cells(ra, "D"))
    rng.Sort key1:=Range("A1"), order1:=xlAscending, _
        Header:=xlYes, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

For i = ra To 3 Step -1
If Cells(i, "A").Value <> Cells(i - 1, "A").Value Then
Cells(i, "A").EntireRow.Insert
End If
Next

End Sub
 
Upvote 0
Hi, apologies I would need some more info, where to put it etc. My table actually has more columns than the example, do I just replace the A and the D in your code?

Thanks for your help.
 
Upvote 0
Hi, apologies I would need some more info, where to put it etc. My table actually has more columns than the example, do I just replace the A and the D in your code?

Thanks for your help.


Yes, if you have data until column H, then:

Code:
Set rng = Range(Cells(1, "A"), Cells(ra, "H"))

This is from: http://www.contextures.com/xlvba01.html
Copy Excel VBA Code to a Regular Module
Instead of starting from scratch, if you need an Excel macro, you can often find sample code at reputable sites on the internet. To copy that code, and add it to one of your workbooks, follow these steps:

  • Copy the sample code that you want to use
  • Open the workbook in which you want to add the code
  • Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
  • Choose Insert | Module
  • Where the cursor is flashing, choose Edit | Paste
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

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