如何通过VBA对Outlook中的excel列进行sorting 代码执行不sorting

我正在parsing通过几千个电子邮件扫描错误。 parsing代码将结果转储成excel未sorting成4列。 A:命中,B:总计,C:百分比,D:用户。

这工作正常。 不过,我想按总点击(或百分比)sorting这些数据,而不必在Excel中手动执行。 这是因为这是生成一个包含多个其他字段组的报告,我想对每个字段进行sorting。

问题是我没有想到能够从前景实际sorting。 代码执行没有错误,但没有任何反应。 我能够使用以下方式成功地在excelmacros中sorting。

Sub test() With ActiveSheet Call .Range("A3:D30").Sort(Key1:=Range("A3"), Order1:=xlDescending, Header:=xlNo) End With End Sub 

然后我把调用线放到我的outlook vba代码中,定义了一切(引用了Excel Object Library)

  With xlSheet If i > 0 Then hitp = Round(hits / i * 100, 1) Else hitp = "0" Dim vstr As Variant Dim temph As String j = 2 .Range("A1:D1").Merge .Range("A1:D1").Value = "Basic Errors" .cells(j, 1).Value = "Total Hits:" .cells(j, 2).Value = "Total Sent:" .cells(j, 3).Value = "Percentage:" .cells(j, 4).Value = "Agent:" For Each vstr In userhit.Keys() j = j + 1 temph = userhit(vstr) If temph = "" Then temph = "0" .cells(j, 1).Value = temph .cells(j, 2).Value = userhit(vstr) + userclean(vstr) .cells(j, 3).Value = Round(userhit(vstr) / (userhit(vstr) + userclean(vstr)) * 100, 1) & "%" .cells(j, 4).Value = vstr DoEvents Next Call .Range("A3:D30").Sort(Key1:=Range("A3"), Order1:=xlDescending, Header:=xlNo) End With 

我正在使用字典(其中14个)来跟踪各种事情,这就是为什么在倾销之前将它们sorting在vba中将会更乏味,尽pipe如果我绝望的话可能是可行的。 可悲的是,这不sortingExcel中的任何东西,尽pipe运行没有错误,如果我把它复制到Excelmacros的工作。

我也看到了另一种做这种类似的方式https://stackoverflow.com/questions/22220127/sorting-excel-range-in-class-module-from-outlook-or-access-vba

 With xlSheet.Sort .SetRange Range((LeftColStr & RowStart & ":" & RightColStr & RowEnd)) .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

但最后,当我尝试时,这对他或我来说都不起作用。 所以我转换到现在的方法认为它会更容易,但事实并非如此。 如果代码在excelmacros中,代码也可以工作,但是尽pipe在outlook中执行,它仍然不起作用。 任何帮助将被大量占用。

所以感谢@ASH为失踪的提示。

它没有它在Excel中工作,因为他们有使用活动工作表的默认值。 现在工作!

 Call .Range("A3:D30").Sort(Key1:=.Range("A3"), Order1:=xlDescending, Header:=xlNo)