Sort list in table then seperate

nparsons75

Well-known Member
Joined
Sep 23, 2013
Messages
1,254
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,

MACHINEITEMQTYDATE
001CAR516/7/17
002BUS
116/7/17
009PLANE216/7/17
004BOAT516/7/17
001SHIP716/7/17
002BIKE316/7/17
009CYCLE116/7/17
004PLANE116/7/17
006BIKE916/7/17
004SHIP316/7/17
006CAR416/7/17

<tbody>
</tbody>


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.

1CAR516/07/2017
1SHIP716/07/2017
2BUS116/07/2017
2BIKE316/07/2017
4BOAT516/07/2017
4PLANE116/07/2017
4SHIP316/07/2017
6BIKE916/07/2017
6CAR416/07/2017
9PLANE216/07/2017
9CYCLE116/07/2017

<colgroup><col width="80" style="width:60pt" span="4"> </colgroup><tbody>
</tbody>

<colgroup><col width="80" style="width:60pt" span="4"> </colgroup><tbody>
</tbody>
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
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,215,757
Messages
6,126,693
Members
449,331
Latest member
smckenzie2016

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