Sort Names according to their column header

Tricia Violeta

New Member
Joined
Aug 2, 2017
Messages
8
Hi, I really need a help right now. I have these thousands of names that I need to sort accordingly. The sample input and desired output is shown below. The 1st column in the desired output must return the names of the people who only went from A to M ONLY (same as the 2nd, 5th and 6th column) while the 3rd column in the desired output must list down the names who traveled from A to M at the same time M to C. (same thing to the 4th row but this time it's vice versa or C to M at the same time M to A)

please i need your help.
51gUO
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Try this for results starting "F7"
NB:- Your data start "A7".

Code:
[COLOR="Navy"]Sub[/COLOR] MG28Aug54
[COLOR="Navy"]Dim[/COLOR] Ray [COLOR="Navy"]As[/COLOR] Variant, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] oMax [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] K [COLOR="Navy"]As[/COLOR] Variant, st [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
Ray = ActiveSheet.Range("a7").CurrentRegion
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] Ac = 1 To 4
    [COLOR="Navy"]For[/COLOR] n = 2 To UBound(Ray, 1)
        [COLOR="Navy"]If[/COLOR] Not IsEmpty(Ray(n, Ac)) [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Not .Exists(Ray(n, Ac)) [COLOR="Navy"]Then[/COLOR]
            .Add Ray(n, Ac), Ray(1, Ac)
        [COLOR="Navy"]Else[/COLOR]
            .Item(Ray(n, Ac)) = .Item(Ray(n, Ac)) & ", " & Ray(1, Ac)
        [COLOR="Navy"]End[/COLOR] If
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] Ac
ReDim nray(1 To .Count, 1 To 6)
nray(1, 1) = "A to M": nray(1, 2) = "M to A": nray(1, 3) = "A to M, M to C"
nray(1, 4) = "C to M, M to A": nray(1, 5) = "M to C": nray(1, 6) = "C to M"
[COLOR="Navy"]For[/COLOR] Ac = 1 To 6
  n = 1
    [COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] K [COLOR="Navy"]In[/COLOR] .keys
        st = IIf(.Item(K) = "M to A, C to M", "C to M, M to A", .Item(K))
        [COLOR="Navy"]If[/COLOR] st = nray(1, Ac) [COLOR="Navy"]Then[/COLOR]
            n = n + 1
            nray(n, Ac) = K
        oMax = Application.Max(oMax, n)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] K
[COLOR="Navy"]Next[/COLOR] Ac
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]With[/COLOR] Range("F7").Resize(oMax, 6)
    .Value = nray
    .Columns.AutoFit
    .Borders.Weight = 2
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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