title: How to get QTP and Excel to sort correctly?
body: <p>I need to sort by columns a ascending, then b ascending, then d ascending and by columns e ascending, then f ascending, then h ascending. Using just QTP, I can't seem to get Excel to sort the data correctly.</p>
  
  <p>What I want:</p>
  
  <pre>
  Table 1:
  | a |  b      |   c   |     d     | e |  f      |   g   |     h     | 
  ---------------------------------------------------------------------
  | 1 | BE      | blank |  51 stuff | 1 | BE      | blank |  51 stuff |
  | 1 | BE      | blank | 100 stuff | 1 | BE      | blank | 100 stuff |
  | 1 | BE OF A | blank | 121 stuff | 1 | BE OF A | blank | 121 stuff |
  | 1 | BE OF A | blank | 200 stuff | 1 | BE OF A | blank | 200 stuff |
  | 2 | SEA     | blank |   5 stuff | 1 | SEA     | blank |   5 stuff |
  </pre>
  
  <p>What I got instead:</p>
  
  <pre>
  Table 2:
  | a |  b      |   c   |     d     | e |  f      |   g   |     h     | 
  ---------------------------------------------------------------------
  | 1 | BE      | blank | 100 stuff | 1 | BE OF A | blank | 121 stuff |
  | 1 | BE      | blank | 51 stuff  | 1 | BE OF A | blank | 200 stuff |
  | 1 | BE OF A | blank | 121 stuff | 1 | BE      | blank | 100 stuff |
  | 1 | BE OF A | blank | 200 stuff | 1 | BE      | blank |  51 stuff |
  | 2 | SEA     | blank |   5 stuff | 1 | SEA     | blank |   5 stuff |
  </pre>
  
  <p>Columns e through h gets populated and sorted first.  Normally, the cells for those columns are populated in the correct sort order seen in Table 1.  However, there have been instances where the sort order is incorrect, but that is rare.  Maybe 1 out of 100,000 tries would it be populated with unsorted data.</p>
  
  <p>Columns a through d gets populated and sorted last.  The cells for those columns are populated in a somewhat haphazard manner.  Table 3 illustrates a very simple end result without forcing a sort.</p>
  
  <pre>
  Table 3:
  | a |  b      |   c   |     d     | e |  f      |   g   |     h     | 
  ---------------------------------------------------------------------
  | 1 | BE      | blank | 100 stuff | 1 | BE      | blank |  51 stuff |
  | 1 | BE      | blank |  51 stuff | 1 | BE      | blank | 100 stuff |
  | 1 | BE OF A | blank | 121 stuff | 1 | BE OF A | blank | 121 stuff |
  | 1 | BE OF A | blank | 200 stuff | 1 | BE OF A | blank | 200 stuff |
  | 2 | SEA     | blank |   5 stuff | 1 | SEA     | blank |   5 stuff |
  </pre>
  
  <p>What's the best way to get QTP and Excel to return the results displayed in Table 1?  Is there even a way to?</p>
  
  <p>Snippet of the code(s) that I'm using:</p>
  
  <pre><code>'Some Code Stuff here which leads to exporting the worksheet
  
  rangeOne = "E1:H" &amp; totalRowCnt
  Set rangeObj = worksheetOne.Range(rangeOne)
  Set range1 = excel1Obj.Range("E1")
  Set range2 = excel1Obj.Range("F1")
  Set range3 = excel1Obj.Range("H1")
  rangeObj.Sort range1, ascend1, range2, ,ascend1, range3, ,ascend1,yes1
  
  'Save worksheet then import sorted data back into Datatable 
  'and add more Code Stuff here which leads to exporting the worksheet again
  
  rangeOne = "A1:D" &amp; totalRowCnt
  Set rangeObj = worksheetOne.Range(rangeOne)
  Set range1 = excel1Obj.Range("A1")
  Set range2 = excel1Obj.Range("B1")
  Set range3 = excel1Obj.Range("D1")
  rangeObj.Sort range1, ascend1, range2, ,ascend1, range3, ,ascend1,yes1
  
  'Save worksheet then end script
  </code></pre>