【问题标题】:Sorting an Excel list using VBA is not working使用 VBA 对 Excel 列表进行排序不起作用
【发布时间】:2012-10-28 17:42:06
【问题描述】:

我正在尝试按名为“名称”的列的字母顺序对一系列列表进行排序。下面的代码编译没有错误,运行没有错误,但它没有对列表进行排序。

任何人都可以帮忙吗?

'**
 ' 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

【问题讨论】:

  • 尝试将可选的 sort_order 作为变体 = xlAscending,我认为将其设置为字符串会被解释为“1”而不是 1
  • 不幸的是没有快乐。即使放置.SortFields.Add sort_column, xlAscending 也会失败,尽管仍然可以运行并且不会导致错误。谢谢。

标签: list sorting excel excel-2007 vba


【解决方案1】:

问题是我没有为SortFields 函数指定哪些参数。将该行更改为以下内容解决了我的问题。

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

【讨论】:

    猜你喜欢
    • 2015-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-30
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多