title: Macro - Multi Level sort by column name
body: <p>I download a set of data from a program that i put into excel. From here, i would like to sort this data in 2 levels. I recorded a macro for this, which serves the basic purpose. </p>
  
  <p>The issue is that I would like the macro to search for the column names (As it's not necessary that the columns would always be in B &amp; F), and sort accordingly. Hence, i would like the code to be such that it looks for the columns titled "Asset Name" &amp; "Action", and sort those. </p>
  
  <p>Here's the macro code that i recorded: (I put in the range of 50000, as i wanted it to be dynamic for no. of rows, instead of static, which recording macros does)</p>
  
  <pre><code>Cells.Select
  ActiveSheet.sort.SortFields.Clear
  ActiveSheet.sort.SortFields.Add Key:=Range("B2:B50000") _
      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  ActiveSheet.sort.SortFields.Add Key:=Range("F2:F50000") _
      , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
  With ActiveSheet.sort
      .SetRange Range("A1:H50000")
      .Header = xlYes
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  </code></pre>
  
  <p>To incorporate the sort by column name, i thought of using the below to assign the column number, but i am not sure where to call in colNum again in the recorded portion of the macro. </p>
  
  <pre><code>Dim colNum As Integer
  colNum = WorksheetFunction.Match("Asset Name", ActiveSheet.Range("1:1"), 0)
  </code></pre>