如何让QTP和Excel正确排序?

时间:2013-04-26 22:42:22

标签: excel vbscript qtp

我需要按列升序排序,然后按升序排序,然后按升序排列,按升序排列,然后按升序排序,然后按升序排序。仅使用QTP,我似乎无法通过Excel对数据进行正确排序。

我想要的是什么:

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 |

我得到了什么:

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 |

首先填充和排序列e到h。通常,这些列的单元格按表1中所示的正确排序顺序填充。但是,有些情况下排序顺序不正确,但这种情况很少见。也许100,000次尝试中有1次会填充未分类的数据。

列a到d最后填充和排序。这些列的单元格以某种随意的方式填充。表3说明了一个非常简单的最终结果,没有强制排序。

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 |

让QTP和Excel返回表1中显示的结果的最佳方法是什么?甚至有办法吗?

我正在使用的代码片段:

'Some Code Stuff here which leads to exporting the worksheet

rangeOne = "E1:H" & 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" & 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

1 个答案:

答案 0 :(得分:0)

尝试将数据加载到Recordset对象中。您可以像这样对记录集进行排序:

rs.Sort = "a ASC, b ASC, d ASC, e ASC, f ASC, h ASC"

然后将已排序的数据复制到Excel或将其写入CSV。

rs.MoveFirst
Do Until rs.EOF
  For i = 0 To rs.Fields.Count - 1
    'copy/write rs.Fields(i).Value
  Next
  rs.MoveNext
Loop