Hi all,
I am trying to sort a table in excel, I have the following code so far:
I know it can be done by key1, key2 and key3 but I did it this way in case I have to add more columns to sort by. I know it would be simple to do without vba, but would like to learn to do it this way.
So this code works all fine for now, but I would like to add a custom sorting order for the basket size sort. I have lots of basket sizes, I don't know the name of them, but I have ones that begin with 05 and I would like those ones to be the first ones, for each first name and family name combo.
I couldn't get it to work in normal excel sort either with the "05*" so I'm not sure how to do this..
I was also thinking the basket size column could be formatted so the cells in it beginning with 05 have a background colour, then do a colour sort? Not sure whats the best way to do this..
(I have done it with the macro recorder but that code seems like an overkill and I am bad at reverse engineering them...)
Please help?
Link to another site where I asked the same question: https://www.excelforum.com/excel-programming-vba-macros/1264622-sorting-with-vba-custom-sort.html
I am trying to sort a table in excel, I have the following code so far:
Code:
[COLOR=#333333]
<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Sub Sort_Table()
Sheet6.ListObjects("Sort_table").Range.Sort Key1:=Sheet6.ListObjects("Sort_table").ListColumns("Basket size").Range, Order1:=xlAscending, Header:=xlYes
Sheet6.ListObjects("Sort_table").Range.Sort Key1:=Sheet6.ListObjects("Sort_table").ListColumns("Family name").Range, Order1:=xlAscending, Header:=xlYes
Sheet6.ListObjects("Sort_table").Range.Sort Key1:=Sheet6.ListObjects("Sort_table").ListColumns("First name").Range, Order1:=xlAscending, Header:=xlYes
End Sub</code>[/COLOR]
So this code works all fine for now, but I would like to add a custom sorting order for the basket size sort. I have lots of basket sizes, I don't know the name of them, but I have ones that begin with 05 and I would like those ones to be the first ones, for each first name and family name combo.
I couldn't get it to work in normal excel sort either with the "05*" so I'm not sure how to do this..
I was also thinking the basket size column could be formatted so the cells in it beginning with 05 have a background colour, then do a colour sort? Not sure whats the best way to do this..
(I have done it with the macro recorder but that code seems like an overkill and I am bad at reverse engineering them...)
Please help?
Link to another site where I asked the same question: https://www.excelforum.com/excel-programming-vba-macros/1264622-sorting-with-vba-custom-sort.html
Last edited: