0

3つのキーでカスタムソートしようとしている範囲があります。シート上の 2 つの異なる列から 2 つの文字列配列を作成し、これらを使用して並べ替え可能な 2 つのカスタム リストを作成しようとしています。

ただし、顧客リストの正確な順序で並べ替える範囲を取得できず、昇順で並べ替えているだけだと思います。

以下は私のコードです:

Sub SortIntoTeams()
Dim LastRow As Long, LastColumn As Long, FormattedRange As Range
LastRow = Sheets(1).Range("B65536").End(xlUp).Row
LastColumn = Sheets(1).Cells(1, Columns.Count).End(xlToLeft).Column
Set FormattedRange = Sheets(1).Range(Cells(8, 1), Cells(LastRow, LastColumn))

Dim SortKey1 As Range, SortKey2 As Range, SortKey3 As Range
Set SortKey1 = Sheets(1).Cells(7, 1)
Set SortKey2 = Sheets(1).Cells(7, 10)
Set SortKey3 = Sheets(1).Cells(7, 3)

Dim sCustomList1() As String, sCustomList2() As String
Dim x As Long, i As Long
ReDim sCustomList1(1 To Sheets(1).Range("A65536").End(xlUp).Row)
ReDim sCustomList2(1 To Sheets(1).Range("E65536").End(xlUp).Row)

For x = 1 To Sheets(1).Range("A65536").End(xlUp).Row
    sCustomList1(x) = Sheets(2).Cells(x, 1)
Next x
For i = 1 To Sheets(1).Range("E65536").End(xlUp).Row
    sCustomList2(i) = Sheets(2).Cells(i, 5)
Next i

Application.AddCustomList ListArray:=sCustomList1
Application.AddCustomList ListArray:=sCustomList2

Sheets(1).Sort.SortFields.Clear
FormattedRange.Sort Key1:=SortKey1, Order1:=xlAscending, Key2:=SortKey2, Order2:=xlAscending, Key3:=SortKey3, Order3:=xlAscending, Header:=xlGuess, _
    OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
    Orientation:=xlTopToBottom, DataOption1:=xlSortNormal

Application.DeleteCustomList Application.CustomListCount

End Sub
4

1 に答える 1

0

ここで役立つ可能性のある 2 つのことに気付きました。

次のように使用される最後の行を取得するセクションを書き直します: Sheets(1).Cells(rows.count,1).end(xlup).row ' これは列 A 用です。 Sheets(1).cells(rows. count,2).end(xlup).row ' これは列 B 用です。

列 E の行には、rows.count,5 を使用します。

また、次のような並べ替え方法を示す必要があるようです: sortmethod:=xlpinyin

于 2013-10-14T23:36:19.303 に答える