title: EXCEL VBA Sort after Populating Data
body: <p>I am new to VBA, and excel macros, but not basic programming.  I have a few dozen excel files, that I am taking data from, cleaning it, and populating it into one file.  After the data is populated, I'd like to sort it according to Column A.  After an 2 hours of playing with it, I just recorded a macro and cut and pasted it into my ButtonCall sub. But I'd like to know why its working and why the solutions I found here, and online would not work for me...</p>
  
  <p>Why does this simple code NOT work:</p>
  
  <pre><code>Set q = ThisWorkbook.Worksheets(2) 
  LastRow = q.UsedRange.rows.Count 'q.UsedRange.Row '  - 1 + q.UsedRange.rows.Count 
  LastCol = q.UsedRange.Columns.Count 
  q.Range("A6:AAA" &amp; LastRow).Sort Key:=q.Columns("A"), Order:=xlDescending
  </code></pre>
  
  <p>While this modified recorded Macro does?</p>
  
  <pre><code>Set q = ThisWorkbook.Worksheets(2)
  LastRow = q.UsedRange.rows.Count 'q.UsedRange.Row '  - 1 + q.UsedRange.rows.Count
  LastCol = q.UsedRange.Columns.Count
  q.Sort.SortFields.Clear
  q.Sort.SortFields.Add Key:=Range("A6:A" &amp; LastRow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With q.Sort
      .SetRange Range("A6:AAA" &amp; LastRow)
      .Header = xlGuess
      .MatchCase = False
      .Orientation = xlTopToBottom
      .SortMethod = xlPinYin
      .Apply
  End With
  </code></pre>
  
  <p>Any thoughts?  Thanks.</p>