【发布时间】:2015-05-04 08:02:55
【问题描述】:
我正在尝试通过 excel VBA 对数据进行排序,但它已经按不同的标题排序。在设置新字段之前如何清除所有排序字段?我试过Sort.SortFields.Clear,但没有奏效。任何帮助将不胜感激。这是我正在使用的代码:
Sheet9.Activate
Columns("J:J").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Cells(1, "J").Value = "InternalID"
Set rng2 = FindHeader("CLIENT NAME", Sheet9.Name)
Count = 1
Sheet9.Cells.Select
With Sheet9.Sort
.SortFields.Clear
.SortFields.Add Key:=rng2, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
【问题讨论】:
-
你确定它们没有被清除吗?它们不是过滤器,因此值不会回到原来的位置,但排序字段会很清晰。
-
也许您也在使用过滤器?在这种情况下,您可能希望以这种方式使用 .ShowAllData:Sheet9.ShowAllData.
-
好的。它正在清除它们。但是当我尝试应用它时,它给了我一个排序参考无效错误。我已经用我正在使用的所有代码更新了这个问题。
-
您错过了
.SetRange电话。 -
MSDN Documentation 可以帮忙吗?