2012-11-08 178 views
2

我想按字母順序排列一系列名爲「Name」的列表。下面的代碼編譯時沒有錯誤,並且運行時沒有錯誤,但是它沒有對列表進行排序。使用VBA對Excel列表排序不起作用

任何人都可以幫忙嗎?

'** 
' Sorts the defined list using the definded parameters 
'' 
Function sort_list(list_name As String, column_name As String, Optional sort_order As String = xlAscending) 

Dim list As ListObject   ' The list that is to be sorted 
Dim sort_column As Range  ' The column in the list that is to be used as the sort key 

    ' Set the list and the sort column 
    Set list = WS_ext.ListObjects(list_name) 
    Set sort_column = list.ListColumns(column_name).Range 

    ' Sort the list 
    With list.Sort 
     .SortFields.Clear 
     .SortFields.Add sort_column, sort_order 
     .Header = xlYes 
     .MatchCase = False 
     .Apply 
    End With 

End Function 
+0

嘗試可選的排序順序爲變種= xlAscending,我覺得這個設置,因爲它得到任何喜悅不幸的是解釋爲「1」,而不是1 – scott

+0

的字符串。即使把'.SortFields.Add sort_column,xlAscending'失敗,儘管仍然運行並且不會導致錯誤。謝謝。 –

回答

2

問題是我沒有指定哪些參數是SortFields函數。將該行更改爲下面的問題解決了我的問題。

.SortFields.Add Key:=sort_column, order:=Sort_order