title: VBA sorting - expand code for 6 columns
body: <p>I was wondering if anyone can help me expand the following code to work on 6 columns. It already works pretty well for any number of rows. How do I add that same construct for columns? Username: assylias constructed this code, and I am trying to adapt it for my sorting needs.</p>
  
  <p>The problem: 
  I need to sort something like this</p>
  
  <pre><code>X A 3
  X B 7
  X C 2
  X D 4
  Y E 8
  Y A 9
  Y B 11
  Y F 2
  </code></pre>
  
  <p>It needs to be sorted as follows: The column where X and Y are represent groups. The letters: A,B,C,D,E,F represent members of the group. The numbers are some metric we are comparing them by. The highest number and the associated member that earned that number is the "leader" of that group, and I want to sort the data so that each leader of each group is compared to each member of that group in the following way:</p>
  
  <pre><code>X  B A 3
  X  B C 2
  X  B D 4
  Y  B E 8
  Y  B A 9
  Y  B F 2
  </code></pre>
  
  <p>Explanation: B happens to be the leader of both groups. I need to compare him to all the other members and to the right of their cell, have a column showing the number that they earned. </p>
  
  <p>Problem: Equipped with Assylias' code, I am now attempting to expand this to my dataset. My dataset has 6 columns, so there are a bunch of qualitative columns to describe each member (like State, ID# etc), and I need help expanding the code to encompass this. Also, if somehow possible, explanations of some of the steps (maybe in form of comments) would enable me to truly connect the dots better. (Mostly, I don't understand what dict1/dict2 are and what they are doing exactly...(dict1.exists(data(i,1)) for example is not obvious to me. </p>
  
  <pre><code>Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  doIt
  End Sub
  Public Sub doIt()
  
  Dim data As Variant
  Dim result As Variant
  Dim i As Long
  Dim j As Long
  Dim dict1 As Variant
  Dim dict2 As Variant
  
  Set dict1 = CreateObject("Scripting.Dictionary")
  Set dict2 = CreateObject("Scripting.Dictionary")
  data = Sheets("Sheet1").UsedRange
  
  For i = LBound(data, 1) To UBound(data, 1)
      If dict1.exists(data(i, 1)) Then
          If dict2(data(i, 1)) &lt; data(i, 3) Then
              dict1(data(i, 1)) = data(i, 2)
              dict2(data(i, 1)) = data(i, 3)
          End If
      Else
          dict1(data(i, 1)) = data(i, 2)
          dict2(data(i, 1)) = data(i, 3)
      End If
  Next i
  
  ReDim result(LBound(data, 1) To UBound(data, 1) - dict1.Count, 1 To 4) As Variant
  
  j = 1
  For i = LBound(data, 1) To UBound(data, 1)
      If data(i, 2) &lt;&gt; dict1(data(i, 1)) Then
          result(j, 1) = data(i, 1)
          result(j, 2) = dict1(data(i, 1))
          result(j, 3) = data(i, 2)
          result(j, 4) = data(i, 3)
          j = j + 1
      End If
  Next i
  
  With Sheets("Sheet2")
      .Cells(1, 5).Resize(UBound(result, 1), UBound(result, 2)) = result
  End With
  </code></pre>
  
  <p>End Sub</p>