0

4 つの異なるシートのエントリを並べ替えるためのマクロがあります。これらのうちの 2 つは左から右に並べ替えたいのですが、他の 2 つは上から下に並べ替える必要があります。現在、コードは大まかに次のようになっています ("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

2 つのブロックの唯一の実際の違いは、「.Orientation」行です。私の質問は、これら 2 つのブロックを結合し、シート名に応じて「.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 に答える