0

我有一个用于对四个不同工作表中的条目进行排序的宏。其中两个我想从左到右排序,而另外两个必须从上到下排序。现在代码大致如下(不要介意“MyRange”和“OtherRange”——它们是在这段代码之外决定的变量):

        If MySheet.Name = "Shippers" _
        Or MySheet.Name = "Consignees" _
        Then
            MySheet.sort.SortFields.Clear
            MySheet.sort.SortFields.Add Key:=Range("MyRange”), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With MySheet.sort
            .SetRange Range(“OtherRange”)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlLeftToRight '<---
            .SortMethod = xlPinYin
            .Apply
        End With

        Else
            MySheet.sort.SortFields.Clear
            MySheet.sort.SortFields.Add Key:=Range("MyRange”), _
            SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With MySheet.sort
            .SetRange Range(“OtherRange”)
            .Header = xlGuess
            .MatchCase = False
            .Orientation = xlTopToBottom '<---
            .SortMethod = xlPinYin
            .Apply
        End With
        End If

两个块之间唯一真正的区别是“.Orientation” - 行。我的问题是:有没有办法合并这两个块并根据工作表名称将“.Orientation”设置为 xlTopToBottom 或 xlLeftToRight?

4

1 回答 1

0

xl~ 常量是Long值,您可以将它们分配给块之前的变量。

Dim OrntValue As Long
Select Case MySheet.Name
    Case "Shippers", "Consignees"
        OrntValue = xlLeftToRight
    Case Else
        OrntValue = xlTopToBottom
End Select
...
.Orientation = OrntValue  

IIf可能是另一种选择,但我更喜欢这个。

于 2013-06-29T12:01:23.943 回答