How to create lists for custom sort in VBA

Waimea

Active Member
Joined
Jun 30, 2018
Messages
465
Office Version
  1. 365
Platform
  1. Windows
Hi, I am currently using custom sort to sort a big list.

I want to apply the custom sort 39 or n times.

Code:
Sub SortCity2()
  
range("A42:H81").Select


ActiveWorkbook.worksheets("Snabb2").Sort.SortFields.Clear


ActiveWorkbook.worksheets("Snabb2").Sort.SortFields.add key:=range("A42"), SortOn:=xlSortOnValues, _
    Order:=xlAscending, CustomOrder:="City2, City1, City3, City4, City5, City6, City7, City8, City9, City10", City11, City12, City13, City14, City15, City16, City17, City18, City19, City20, City21, City22, City23, City24, City25, City26, City27, City28, City29, City30, City31, City32, City33, City34, City35, City36, City37, City38, City39, City40", DataOption:=xlSortNormal
    
With ActiveWorkbook.worksheets("Snabb2").Sort
.SetRange range("A42:H81")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub

The sorted list contains 40 * 40 cities for a total of 1600 rows + 1 header row.

The list starts with City1 to City40, then it starts over at City2, City1, City3, ..., City40.

City1, City2, City3, City4, City5, City6, City7, City8, City9, City10", City11, City12, City13, City14, City15, City16, City17, City18, City19, City20, City21, City22, City23, City24, City25, City26, City27, City28, City29, City30, City31, City32, City33, City34, City35, City36, City37, City38, City39, City40

City2, City1, City3, City4, City5, City6, City7, City8, City9, City10", City11, City12, City13, City14, City15, City16, City17, City18, City19, City20, City21, City22, City23, City24, City25, City26, City27, City28, City29, City30, City31, City32, City33, City34, City35, City36, City37, City38, City39, City40

City3, City1, City2, City4, City5, City6, City7, City8, City9, City10", City11, City12, City13, City14, City15, City16, City17, City18, City19, City20, City21, City22, City23, City24, City25, City26, City27, City28, City29, City30, City31, City32, City33, City34, City35, City36, City37, City38, City39, City40

City4, City1, City2, City3, City5, City6, City7, City8, City9, City10", City11, City12, City13, City14, City15, City16, City17, City18, City19, City20, City21, City22, City23, City24, City25, City26, City27, City28, City29, City30, City31, City32, City33, City34, City35, City36, City37, City38, City39, City40

Edit: In my workbook the city names doesn't contain the numbers after the cities.
 
Last edited:

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Perhaps a sub or function that passes the array, the number of items and the nth position would work?

I don't know however where to start to create such a sub or function?

Code:
Function CUSTOMSORT(myArray As Array, numberOfItems as Integer, nthPosition As Integer) As Variant
' code to change the value of the 1 first row to the nth position

End function
 
Last edited:
Upvote 0
Could someone give me some pointers on how to go about creating a sub that places the nth element first in a list?
 
Upvote 0
I have created an array with 40 items in it, with index at 0.

I can access the arrays positions by myarray(0) to myarray(39).

I want to use the array with the nth item as myarray(nth).

I guess I could create 40 arrays but that seems like a lot of manual work?
 
Upvote 0
I don't know how to use custom sort to solve your problem.
How about using a temporary helper column?
I can write a macro to insert a sequence of numbers into the helper column so you can sort data according to your requirement above.
 
Upvote 0
Hi Akuini,

thank you for your reply! I am not using custom sort to solve this, I am trying to create automated lists to use with custom sort.

Can you explain how you would solve it using a helper column?
 
Upvote 0
Hi Akuini,

thank you for your reply! I am not using custom sort to solve this, I am trying to create automated lists to use with custom sort.

Can you explain how you would solve it using a helper column?

For example try this:

Code:
Sub a1081405a()
'https://www.mrexcel.com/forum/excel-questions/1081405-how-create-lists-custom-sort-vba.html
Dim i As Long, n As Long, m As Long, k As Long, c As Long
Dim x As String
Dim va As Variant, z As Variant
Dim dar As Object

va = Range("A1", Cells(Rows.count, "A").End(xlUp))
Set dar = CreateObject("System.Collections.Sortedlist")
    For i = 1 To UBound(va, 1)
        If Not dar.contains(va(i, 1)) Then
         dar.Add va(i, 1), 1
        End If
    Next

For c = 0 To dar.count - 1
    n = 10101 + m
        For k = 0 To dar.count - 1
             z = dar.GetKey(k)
                For i = 1 To UBound(va, 1)
                    If va(i, 1) = z Then
                        va(i, 1) = n
                        n = n + 1
                        Exit For
                    End If
                Next
        Next
    m = m + 100
Next

For i = 1 To UBound(va, 1)
    x = CStr(va(i, 1))
    If Mid(x, 2, 2) = Mid(x, 4, 2) Then
        va(i, 1) = Left(x, Len(x) - 1) & "0"
    End If
Next

Range("C1").Resize(UBound(va, 1), 1) = va

End Sub


EXAMPLE:


Book1
A
1Harry
2Mark
3Mark
4Harry
5Aaden
6Aaden
7Leandro
8Leandro
9Aaden
10Harry
11Leandro
12Leandro
13Aaden
14Mark
15Harry
16Mark
Sheet1


AFTER RUNNING THE MACRO:


Book1
ABC
1Harry10102
2Mark10104
3Mark10204
4Harry10200
5Aaden10100
6Aaden10201
7Leandro10103
8Leandro10203
9Aaden10301
10Harry10302
11Leandro10300
12Leandro10403
13Aaden10401
14Mark10304
15Harry10402
16Mark10400
Sheet1


YOU CAN SORT BY COLUMN C, THE RESULT:


Book1
ABC
1Aaden10100
2Harry10102
3Leandro10103
4Mark10104
5Harry10200
6Aaden10201
7Leandro10203
8Mark10204
9Leandro10300
10Aaden10301
11Harry10302
12Mark10304
13Mark10400
14Aaden10401
15Harry10402
16Leandro10403
Sheet1
 
Upvote 0
Hi, thank you for your reply! I am not sure I get what you are doing, you are creating a helper column with unique rankings in them but after that I don't understand?

I want to run custom sort 39 or 40 times, each time I want the first item/city/name to change.

Ex.

First custom sort:
Aaden (1), Harry (2), Leandro (3), Mark (4), Harry (5)

Second custom sort:
Harry (2), Aaden (1), Leandro (3), Mark (4), Harry (5)

Third custom sort:
Leandro (3), Aaden (1), Harry (2), Mark (4), Harry (5)

Fourth custom sort:
Mark (4), Aaden (1), Harry (2), Leandro (3), Harry (5)

Fifth custom sort:
Harry (5), Aaden (1), Harry (2), Leandro (3), Mark (4)

EDIT: I got it wrong!
 
Last edited:
Upvote 0
The sorted list contains 40 * 40 cities for a total of 1600 rows + 1 header row.

Hm, I might misunderstand something
You have data that contains 40 unique cities in 1600 rows, right?
 
Upvote 0
Hi, I think it is my fault that I explained it poorly and I apologize.

I have 1600 rows where the city1 to city40 repeats in batches of 40. I am using VBA to insert formulas into my worksheet and for every 40th row I want to change the reference what is being used in the formulas.

Perhaps you could help me to figure out how to offset
Code:
"=SUMPRODUCT($B$2:$H$2,B2:H2)"
so that it increases with 40 rows but with 40 + 1 rows for the reference. The reference is B2:H2 for the first 40 rows, the reference is B42:H42 for the next 40 rows and so on.

Name Metric1 Metric2 Metric3
City1657195City1 is at the top
City245631
City3669116
City4287054
City5301219
City676342
City7742447
City804629
City955624
City10679335
City1185112
City12351689
City13399629
City14399369
City15609100
City1612502
City17779799
City18422112
City1913955
City20132010
City21686933
City22708920
City2351824
City2433015
City2597698
City26715934
City27445497
City28353113
City29807796
City30421590
City31256613
City3239893
City33636020
City34776490
City3542366
City36838963
City3727757
City3806064
City39736929
City40329128
City2548519City2 is at the top
City1423032
City378120
City4634468
City5138595
City6421681
City72182
City878869
City989112
City10908752
City1160113
City12768554
City13652878
City14515472
City15585010
City16217285
City17816258
City18143010
City1925810
City2041419
City21862050
City22533654
City239210055
City24208571
City25129769
City2628391
City27286836
City28629347
City29392528
City30935141
City31451799
City32978997
City33913172
City34186740
City35597892
City36738980
City3774085
City3830033
City39668190
City4043386
City3671144City3 is at the top
City1214855
City2255231
City4859060
City555748
City6993565
City7488021
City885592
City9714449
City1047313
City1127283
City12919144
City13915928
City14982440
City15184732
City16888338
City1781370
City18797911
City19295286
City201004672
City21404820
City22519159
City23993942
City2457896
City25767396
City262268
City271510091
City28726215
City29562479
City30963595
City31313717
City3286170
City33725855
City34276019
City3517434
City36456217
City3722786
City38763145
City3934782
City40758499
City47365City4 is at the top
City1738969
City2584870
City336934
City5767086
City69491100
City7385322
City8334821
City9198100
City10989854
City11714591
City12146895
City13716994
City14245399
City15694925
City16421878
City1766894
City18819868
City19439815
City20477348
City21708249
City22296539
City23384091
City248010079
City25836599
City26661391
City27523529
City28624856
City29837088
City30416443
City31286412
City32608751
City33332940
City3434230
City3593533
City3629647
City37979216
City38452461
City39365570
City40372766
etc

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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