Help with macro to custom sort?

Coyotex3

Active Member
Joined
Dec 12, 2021
Messages
496
Office Version
  1. 365
Platform
  1. Windows
Hello guys can you guys possibly help me sort a range/table(open to whichever is easier)

Book1
BCD
1NamesDescriptionID
2John CenaYou Can't See Me- John Cena5
3The UndertakerTime to Play The Game- Triple H6
4FunakiHeartbreak Kid- Shawn Michaels8
5RicoCan you Smell- The Rdock7
6EugeneI don't Know-Eugene4
7Triple HSmackDown's number one Announcer- Funaki2
8The RockRest in Peace- The Undertaker3
9Shawn Michaels Kiss me- Rico1
10That's the Bottom Line- Stone Cold9
11Here Comes the Pain- Brock Lesnar10
Sheet1


I would like to sort to it to look like this

Book1
BCD
15NamesDescriptionID
16John CenaYou Can't See Me- John Cena5
17FunakiSmackDown's number one Announcer- Funaki2
18The RockCan you Smell- The Rdock7
19
20
Sheet1


Essentially matching the name on Column B with the similar name in column C and sort while keeping columns C and D aligned
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
The 3rd one can pose a challenge as The Rock is not present in the text Can you Smell- The Rdock. And if you consider Rock and Rdock similar then that can give you lot of false positives.

If you are just looking for the exact presense of column B in column C then that is possible using Autofilter - Contains.
 
Upvote 0
VBA Code:
Sub CustomS()
     Dim f()
     Set dict = CreateObject("scripting.dictionary")
     dict.Add dict.Count, Array("Names", "Description", "ID")

     Set c = Range(Range("B2"), Range("B2").End(xlDown))
     mynames = c.Value                                          'array with the names
     Set c = Range(Range("C2"), Range("C2").End(xlDown)).Resize(, 2)
     mydescriptions = c.Value                                   'array with the description & ID
     kol_c = Application.Transpose(Application.Index(mydescriptions, 0, 1))     'array with only the description

     For i = 1 To UBound(mynames)                               'loop through names
          fl = Filter(kol_c, mynames(i, 1), 1, vbTextCompare)   'filter description on the name
          If UBound(fl) <> -1 Then                              'at least 1 matching ?
               For j = 0 To UBound(fl)                          'loop through matches
                    r = Application.Match(fl(j), kol_c, 0)      'find according row in kol_C
                    dict.Add dict.Count, Array(mynames(i, 1), mydescriptions(r, 1), mydescriptions(r, 2))     'add data to dictionary
               Next
          End If
     Next

     With Range("F1")                                           'outputrange
          .Resize(100, 3).ClearContents                         'clear former data
          .Resize(dict.Count, 3).Value = Application.Index(dict.items, 0, 0)     'write new data
     End With
End Sub
 
Upvote 0
Solution
The 3rd one can pose a challenge as The Rock is not present in the text Can you Smell- The Rdock. And if you consider Rock and Rdock similar then that can give you lot of false positives.

If you are just looking for the exact presense of column B in column C then that is possible using Autofilter - Contains.
Thank you for picking up on that typo. I am looking for the exact name presence yes. I will have to look into AutoFilters as I do not know much.
 
Upvote 0
VBA Code:
Sub CustomS()
     Dim f()
     Set dict = CreateObject("scripting.dictionary")
     dict.Add dict.Count, Array("Names", "Description", "ID")

     Set c = Range(Range("B2"), Range("B2").End(xlDown))
     mynames = c.Value                                          'array with the names
     Set c = Range(Range("C2"), Range("C2").End(xlDown)).Resize(, 2)
     mydescriptions = c.Value                                   'array with the description & ID
     kol_c = Application.Transpose(Application.Index(mydescriptions, 0, 1))     'array with only the description

     For i = 1 To UBound(mynames)                               'loop through names
          fl = Filter(kol_c, mynames(i, 1), 1, vbTextCompare)   'filter description on the name
          If UBound(fl) <> -1 Then                              'at least 1 matching ?
               For j = 0 To UBound(fl)                          'loop through matches
                    r = Application.Match(fl(j), kol_c, 0)      'find according row in kol_C
                    dict.Add dict.Count, Array(mynames(i, 1), mydescriptions(r, 1), mydescriptions(r, 2))     'add data to dictionary
               Next
          End If
     Next

     With Range("F1")                                           'outputrange
          .Resize(100, 3).ClearContents                         'clear former data
          .Resize(dict.Count, 3).Value = Application.Index(dict.items, 0, 0)     'write new data
     End With
End Sub
This is brilliant mate! Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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