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 に設定する方法はありますか?