How to sort data based on multiple columns

Uumas

New Member
Joined
Oct 8, 2017
Messages
3
I'll just give an example:
ABCDEFGHIJKL
11a8o1a
28b10p8b8o
311c11q10p
414d13r11c11q
516e14s13r
623f19t14d14s
723u16e
819t
923f23u
10
11
12

<tbody>
</tbody>

I have data like in columns A-D. I'd like to sort them based on the values of columns A and C so it looks like columns F-I. How should I go about doing this? Thank you!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try this for results starting "G1"

Code:
[COLOR="Navy"]Sub[/COLOR] MG13Oct28
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Dic [COLOR="Navy"]As[/COLOR] Object, Ray [COLOR="Navy"]As[/COLOR] Variant, Ac [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Dim[/COLOR] Q [COLOR="Navy"]As[/COLOR] Variant, nRay [COLOR="Navy"]As[/COLOR] Variant, c [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
Ray = Cells(1).CurrentRegion
[COLOR="Navy"]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
 Dic.CompareMode = vbTextCompare
    [COLOR="Navy"]With[/COLOR] CreateObject("System.Collections.ArrayList")


[COLOR="Navy"]For[/COLOR] Ac = 1 To 3 [COLOR="Navy"]Step[/COLOR] 2
    [COLOR="Navy"]For[/COLOR] n = 1 To UBound(Ray, 1)
        [COLOR="Navy"]If[/COLOR] Not .Contains(Ray(n, Ac)) [COLOR="Navy"]Then[/COLOR] .Add Ray(n, Ac)
           [COLOR="Navy"]If[/COLOR] Not Dic.Exists(Ray(n, Ac)) [COLOR="Navy"]Then[/COLOR]
               ReDim TRay(1 To 3)
                TRay(Ac) = Ray(n, Ac + 1)
                
                Dic.Add Ray(n, Ac), TRay
           [COLOR="Navy"]Else[/COLOR]
                Q = Dic(Ray(n, Ac))
                Q(Ac) = Ray(n, Ac + 1)
                Dic(Ray(n, Ac)) = Q
          [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]Next[/COLOR] Ac
     .Sort: nRay = .toarray


ReDim nSort(1 To UBound(Ray, 1) * 2, 1 To UBound(Ray, 2))
[COLOR="Navy"]For[/COLOR] n = 1 To UBound(nRay)
    c = c + 1
    [COLOR="Navy"]If[/COLOR] Dic(nRay(n))(1) <> "" [COLOR="Navy"]Then[/COLOR]
        nSort(c, 1) = nRay(n)
        nSort(c, 2) = Dic(nRay(n))(1)
    [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]If[/COLOR] Dic(nRay(n))(3) <> "" [COLOR="Navy"]Then[/COLOR]
        nSort(c, 3) = nRay(n)
        nSort(c, 4) = Dic(nRay(n))(3)
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] With


[COLOR="Navy"]With[/COLOR] Range("G1").Resize(c, UBound(Ray, 2))
    .Value = nSort
    .Borders.Weight = 2
    .HorizontalAlignment = xlCenter
[COLOR="Navy"]End[/COLOR] With

[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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