2013-10-14 35 views
0

我有一個範圍,我試圖用3個鍵自定義排序。 我想從一個工作表上的兩個不同的列構造兩個字符串數組,並使用它們來創建兩個可以排序的自定義列表 - 第三個鍵只是通過標準鍵進行排序。用3個鍵自定義排序

但是,我不能按照我的客戶列表的確切順序排序範圍,我相信它只是按升序排序。

這裏是我下面的代碼:

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 

回答

0

我注意到兩件事情,可以幫助這裏。

重寫獲取的最後一行的章節中使用這樣的: 表(1).Cells(rows.count,1).END(xlup).row'這將是A列 表(1 ).cells(rows.count,2).END(xlup).row'這將是B列

你會使用rows.count,5爲那些在列E.

此外,它出現您需要指明排序方法,例如:sortmethod:= xlpinyin