Sort according to custom list

urimagic

New Member
Joined
Jun 1, 2023
Messages
16
Office Version
  1. 2013
Platform
  1. Windows
Hi, my first time here. First of all, I would like to point out that I posted this problem with Excel help...The link is here..Sort according to custom list

I did get help but the code I was requesting did not work on my pc....only to find out a bit later I had by chance indicated I was using Excel 2013. That was a huge mistake...my other pc has Excel 2013, but the pc is malfunctioning, therefore me using this one....It has Excel 2007 installed. I replied back but I have not received response....So I'm trying here. I have downloaded and installed XL2BB, but I'm not able to work it...I'll be sure to check it out and hopefully next time I will be able to use it.
Sort.png

Okay, In column A is data going in. Column C is the sort list I want column A to automatically sort by. So as I enter data into column A, the data must sort automatically to look like the data set in column E. Column G represents the updated list once I had added for example, 1 Pear 2 in column A. Thanks for all the help and assistance. Appreciated.
 
Assuming your Sort By List is in column D per you post #9 and that Column B is empty, and ignoring your New List and New List 2 which need more exlanation, try this as a stand alone macro in the a standard module.
Please run it on a copy of your workbook it will overwrite column B is there is anything it.

If you want to automate it we can modify it to go into a worksheet change event but it might be a nuisance for it to run every time you make an entry.

VBA Code:
Sub SortByList()

    Dim sht As Worksheet
    Dim sortbyRng As Range, srcRng As Range
    Dim sortbyLastRow As Long, srcLastRow As Long
    Dim sortbyArr As Variant, srcArr As Variant
    Dim dictSortBy As Object, dictKey As String
    Dim i As Long
    
    Set sht = ActiveSheet
    With sht
        sortbyLastRow = .Range("D" & Rows.Count).End(xlUp).Row
        Set sortbyRng = .Range("D1:D" & sortbyLastRow)
        sortbyArr = sortbyRng.Value
        
        srcLastRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set srcRng = .Range("A1:A" & srcLastRow)
        srcArr = srcRng.Value
        ' Assume column B is empty
        ReDim Preserve srcArr(1 To UBound(srcArr, 1), 1 To 2)
    End With


    Set dictSortBy = CreateObject("Scripting.dictionary")
    
    ' Load sort by range into Dictionary
    For i = 1 To UBound(sortbyArr)
        dictKey = sortbyArr(i, 1)
        If Not dictSortBy.exists(dictKey) Then
            dictSortBy(dictKey) = i
        End If
    Next i
    
    For i = 1 To UBound(srcArr)
        dictKey = Left(srcArr(i, 1), InStrRev(srcArr(i, 1), " ") - 1)
        If dictSortBy.exists(dictKey) Then
            srcArr(i, 2) = dictSortBy(dictKey)
        Else
            srcArr(i, 2) = "Not in List"
        End If
    
    Next i
    
    Set srcRng = srcRng.Resize(, 2)
    srcRng.Columns(2).Value = Application.Index(srcArr, 0, 2)
    
    srcRng.Sort key1:=srcRng.Cells(1, 2), _
                Order1:=xlAscending, _
                Header:=xlNo
                
    srcRng.Columns(2).ClearContents
    
End Sub
 
Upvote 0
Solution

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Well, that explains why the Custom Sort hasn't been working for you (I'll ignore the fact that the column containing the sort list is D and not C as per all your samples & descriptions to date because you must have adjusted the code to suit, otherwise you'd have gotten an error message). I'm not sure that you understand how a Custom sort works - suggest you have a look at this: Sort data using a custom list - Microsoft Support. As it stands, there isn't a single item in your column A that matches any item in column D - therefore, the Custom sort would have had zero effect. Not sure that I can help you any further on this. Good luck & best wishes.
Thanks for your feedback,

well, no the reason for nothing working is not because of different columns, etc....I know how to at least adjust a code to get what I want..I mean, as far as changing ranges is concerned, at least...I was just hoping to get a code that would look at the text portion of the input value, for example 1 Pet 2:5..so it finds "Pet", and then moves that data to the range of "Pet" as it is in the sort list, so the code determines what the "Pet" data must be before, and what it must be after, in the main list, and then puts it there. What I mean is this..lets say there are only 2 inputs in column A, lets say Exo 2:6 and Mat 3:9..for example, and I input Sag 5:2, The code must look at the sort list to "see" where SAG 5:2 must go between...then sees that it goes between Exo and Mat, so it inserts the data between those two. If I put Gen 3:6, the code realises that GEN goes right on top, and puts it on top in column A. Now a lst note, should I put Sag 4:8, moves that data to the "Sag" already in column A, but also above Sag 5:2....This was what I really wanted...don't know if you are still willing to see if you can help me....please?
 
Upvote 0
Assuming your Sort By List is in column D per you post #9 and that Column B is empty, and ignoring your New List and New List 2 which need more exlanation, try this as a stand alone macro in the a standard module.
Please run it on a copy of your workbook it will overwrite column B is there is anything it.

If you want to automate it we can modify it to go into a worksheet change event but it might be a nuisance for it to run every time you make an entry.

VBA Code:
Sub SortByList()

    Dim sht As Worksheet
    Dim sortbyRng As Range, srcRng As Range
    Dim sortbyLastRow As Long, srcLastRow As Long
    Dim sortbyArr As Variant, srcArr As Variant
    Dim dictSortBy As Object, dictKey As String
    Dim i As Long
   
    Set sht = ActiveSheet
    With sht
        sortbyLastRow = .Range("D" & Rows.Count).End(xlUp).Row
        Set sortbyRng = .Range("D1:D" & sortbyLastRow)
        sortbyArr = sortbyRng.Value
       
        srcLastRow = .Range("A" & Rows.Count).End(xlUp).Row
        Set srcRng = .Range("A1:A" & srcLastRow)
        srcArr = srcRng.Value
        ' Assume column B is empty
        ReDim Preserve srcArr(1 To UBound(srcArr, 1), 1 To 2)
    End With


    Set dictSortBy = CreateObject("Scripting.dictionary")
   
    ' Load sort by range into Dictionary
    For i = 1 To UBound(sortbyArr)
        dictKey = sortbyArr(i, 1)
        If Not dictSortBy.exists(dictKey) Then
            dictSortBy(dictKey) = i
        End If
    Next i
   
    For i = 1 To UBound(srcArr)
        dictKey = Left(srcArr(i, 1), InStrRev(srcArr(i, 1), " ") - 1)
        If dictSortBy.exists(dictKey) Then
            srcArr(i, 2) = dictSortBy(dictKey)
        Else
            srcArr(i, 2) = "Not in List"
        End If
   
    Next i
   
    Set srcRng = srcRng.Resize(, 2)
    srcRng.Columns(2).Value = Application.Index(srcArr, 0, 2)
   
    srcRng.Sort key1:=srcRng.Cells(1, 2), _
                Order1:=xlAscending, _
                Header:=xlNo
               
    srcRng.Columns(2).ClearContents
   
End Sub
Hallo Alex Blakenburg,

THANK YOU KINDLY...the code works 100%...I do appreciate this a heck of a lot....I am really grateful..Thanks
 
Upvote 0
You're welcome. Glad we could help.
Your sample on post #9 made the patterns in the data much clearer and we wouldn't have gotten to that point without @kevin9999's contribution.
 
Upvote 0
You're welcome. Glad we could help.
Your sample on post #9 made the patterns in the data much clearer and we wouldn't have gotten to that point without @kevin9999's contribution.
Absolutely, I agree 100%...next time I'll just post original work as opposed to a dummy sheet with dummy data....thank you...stay blessed.
 
Upvote 0
You're welcome. Glad we could help.
Your sample on post #9 made the patterns in the data much clearer and we wouldn't have gotten to that point without @kevin9999's contribution.
Hi Alex Blakenburg,

Sorry to trouble you again...I have noticed 5 entries just refuse to sort. I have highlighted them in yellow. I re-typed them, however for some reason they just do not sort...Would you kindly please check maybe why that is?..please?..

Tekse sort.xlsm
AD
1Num 23:19Gen
2Jes 29:13Exo
3Mig 6:8Lev
4Luk 9:23Num
5Joh 1:12Deu
6Joh 3:3Jos
7Joh 8:32Rig
8Joh 4:23Rut
9Joh 4:241 Sam
10Joh 14:62 Sam
11Rom 3:231 Kon
12Rom 4:252 Kon
13Rom 5:121 Kro
14Rom 5:182 Kro
15Rom 6:6Esr
16Rom 6:23Neh
17Rom 8:1Est
18Rom 8:5Job
19Rom 6:2Psa
20Rom 6:3Spr
21Rom 6:4Pre
22Rom 6:1Hoo
232 Kor 5:17Jes
24Gal 2:20Jer
25Efe 1:3Kla
26Efe 2:8Ese
27Efe 2:9Dan
28Kol 2:13Hos
29Kol 2:14Joe
30Kol 2:15Amo
312 Tim 3:5Oba
32Heb 4:16Jon
33Heb 11:1Mig
34Heb 11:6Nah
35Heb 13:5Hab
36Jak 1:2Sef
37Jak 1:3Hag
38Jak 1:4Sag
39Jak 1:5Mal
40Jak 1:6Mat
41Jak 1:7Mar
42Jak 2:19Luk
43Jak 2:20Joh
44Jak 4:7Han
45Jak 5:14Rom
46Jak 5:151 Kor
47Jak 5:162 Kor
481 Pet 3:18Gal
491 Pet 5:7Efe
501 Pet 5:8Fill
511 Joh 2:6Kol
521 Joh 5:41 The
531 Joh 5:52 The
541 Tim
552 Tim
56Tit
57Fil
58Heb
59Jak
601 Pet
612 Pet
621 Joh
632 Joh
643 Joh
65Jud
66Ope
Sheet1
 
Last edited:
Upvote 0
@urimagic try this:
I assumed:
1. Data in col A always has numbers at the end, separated by ":" , such as 3:34, 45:67 etc.
2, Col B:C is blank, they are used as temporary helper column:
VBA Code:
Sub urimagic1()
Dim va, vb
Dim c As Range

Set c = Range("A1", Cells(Rows.Count, "A").End(xlUp)).Resize(, 3)
va = c.Value
vb = Range("D1", Cells(Rows.Count, "D").End(xlUp))

For i = 1 To UBound(va, 1)
    For j = 1 To UBound(vb, 1)
        If InStr(va(i, 1), vb(j, 1)) = 1 Then
            va(i, 2) = j
            Exit For
        End If
    Next
Next

For i = 1 To UBound(va, 1)
        a = Split(va(i, 1), " ")
        b = Split(a(UBound(a)), ":")
        va(i, 3) = Format(b(0), "0000") & "|" & Format(b(1), "0000")
Next

Range("A1").Resize(UBound(va, 1), 3) = va

c.Sort Key1:=c.Columns(2), Order1:=xlAscending, Key2:=c.Columns(3), Order2:=xlAscending, Header:=xlNo
c.Offset(, 1).Resize(, 2).ClearContents
End Sub

Book1
ABCD
1Num 23:19Gen
2Jes 29:13Exo
3Mig 6:8Lev
4Luk 9:23Num
5Joh 1:12Deu
6Joh 3:3Jos
7Joh 4:23Rig
8Joh 4:24Rut
9Joh 8:321 Sam
10Joh 14:62 Sam
11Rom 3:231 Kon
12Rom 4:252 Kon
13Rom 5:121 Kro
14Rom 5:182 Kro
15Rom 6:1Esr
16Rom 6:2Neh
17Rom 6:3Est
18Rom 6:4Job
19Rom 6:6Psa
20Rom 6:23Spr
21Rom 8:1Pre
22Rom 8:5Hoo
232 Kor 5:17Jes
24Gal 2:20Jer
25Efe 1:3Kla
26Efe 2:8Ese
27Efe 2:9Dan
28Kol 2:13Hos
29Kol 2:14Joe
30Kol 2:15Amo
312 Tim 3:5Oba
32Heb 4:16Jon
33Heb 11:1Mig
34Heb 11:6Nah
35Heb 13:5Hab
36Jak 1:2Sef
37Jak 1:3Hag
38Jak 1:4Sag
39Jak 1:5Mal
40Jak 1:6Mat
41Jak 1:7Mar
42Jak 2:19Luk
43Jak 2:20Joh
44Jak 4:7Han
45Jak 5:14Rom
46Jak 5:151 Kor
47Jak 5:162 Kor
481 Pet 3:18Gal
491 Pet 5:7Efe
501 Pet 5:8Fill
511 Joh 2:6Kol
521 Joh 5:41 The
531 Joh 5:52 The
541 Tim
552 Tim
56Tit
57Fil
58Heb
59Jak
601 Pet
612 Pet
621 Joh
632 Joh
643 Joh
65Jud
66Ope
Sheet1
 
Upvote 0
@urimagic - assuming 1:2 means 1 hr 2 mins, then @Akuini's code will do what you seem to be trying to do.
Although using a dictionary will be faster in some scenarios, since Akuini's code completes in under 0.5 secs for 10k rows given your example sort list in column D, it would be hardly noticeable.
If performance does become an issue let us know and I can look at modifying my dictionary solution to sort on the time component as a secondary sort.
 
Upvote 0
@urimagic - assuming 1:2 means 1 hr 2 mins, then @Akuini's code will do what you seem to be trying to do.
Although using a dictionary will be faster in some scenarios, since Akuini's code completes in under 0.5 secs for 10k rows given your example sort list in column D, it would be hardly noticeable.
If performance does become an issue let us know and I can look at modifying my dictionary solution to sort on the time component as a secondary sort.
Hi Alex Blakenburg,

Splendid work, thank you so very much...performance really is currently not an issue, thank you...I honestly appreciate your time with this...stay blessed..
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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