【问题标题】:'1004': "The sort reference is not valid."“1004”:“排序引用无效。”
【发布时间】:2013-03-08 14:34:22
【问题描述】:

我正在尝试在单独的工作表中对范围进行排序。 但是,我不断收到此消息:

 '1004': "The sort reference is not valid. Make sure it's within the data you want to sort, and the first Sort By box isn't the same or blank. 

我检查了范围,它们都存在并且正在工作。

代码如下:

Dim EmpBRange As String

EmpBRange = Sheets("EmployeeData").Cells(Cells.Rows.Count, "B").End(xlUp).Row

Worksheets("EmployeeData").Range("K3:K" & EmpBRange).Sort Key1:=Range("K3:K" & EmpBRange), Order1:=xlAscending, Header:=xlGuess, _
       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
       DataOption1:=xlSortNormal

提前致谢

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    我怀疑您需要完全限定 Key1 范围,因为您正在从不同的工作表中调用代码:

    Worksheets("EmployeeData").Range("K3:K" & EmpBRange).Sort Key1:=Worksheets("EmployeeData").Range("K3:K" & EmpBRange)
    

    这通常是个好主意。

    【讨论】:

    • 手动输入此代码时,VBA 编辑器建议使用xlSortRowsxlSortColumns 作为方向,并且在其建议中不包括xlTopToBottomxlTopToBottom = xlSortColumns不是 xlSortRows。我对此感到困惑,这导致了我的错误。
    【解决方案2】:

    我一直在尝试使用 Sort 方法,但来自 Powershell。我只有The sort reference is not valid 部分没有Make sure it's within the data you want to sort, and the first Sort By box isn't the same or blank 部分。我就是这样过来的。

    我的问题是由于忽略了 Sort 调用的参数。如果您仔细查看文档,您会发现在键和命令参数中间隐藏了一个 Type 参数:

    表达式.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod, DataOption1, DataOption2, DataOption3)

    我已经通过了$null 并且我的方法调用开始工作了。下一个奇怪的事情是,由于某种原因,Key2 / Order2 被忽略了。我正在使用所有 3 个键对数据进行排序。对此的解决方法是在方法调用中将Key2 / Order2Key3 / Order3 参数交换。奇怪的是,它奏效了。

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多