vba '1004':“排序引用无效。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15296400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:06:43  来源:igfitidea点击:

'1004': "The sort reference is not valid."

excelvba

提问by Samantha Monti

I am trying to sort a range within a separate sheet. However, I keep getting this message:

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

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

I have checked the ranges and they all exist and are working.

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

The code is below:

代码如下:

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

Thanks in advance

提前致谢

回答by Doug Glancy

I suspect you need to fully qualify the Key1range, because you are calling the code from a different sheet:

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

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

This is generally a good idea.

这通常是一个好主意。

回答by kellogs

I have been trying to use the Sortmethod but from Powershell. And I only got The sort reference is not validpart without the Make sure it's within the data you want to sort, and the first Sort By box isn't the same or blankpart. This is how I got here.

我一直在尝试使用该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一部分。这就是我到这里的方式。

My issue was due to having disregarded an argument to the Sortcall. If you look closer to the documentation you will see there is a Typeparameter tucked away in the middle of the key and order params:

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

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

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

I have passed $nullfor that one and my method call started working. The next strange thing was that for some reason Key2 / Order2was disregarded. I am using all 3 keys to sort my data. The fix to that was to swap Key2 / Order2with Key3 / Order3arguments within the method call. Strangely, it has worked.

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