“1004”:“sorting参考无效”。

我正在尝试在一个单独的工作表中排列一个范围。 不过,我不断收到这个消息:

'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 

提前致谢

我怀疑你需要完全限定Key1范围,因为你是从另外一个表格调用代码:

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

这通常是一个好主意。

我一直在尝试使用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参数:

expression式。sorting(Key1,Order1,Key2,Type,Order2,Key3,Order3,Header,OrderCustom,MatchCase,Orientation,SortMethod,DataOption1,DataOption2,DataOption3)

我已经传递了$null ,我的方法调用开始工作。 接下来奇怪的是由于某种原因, Key2 / Order2被忽略了。 我使用所有3个键来sorting我的数据。 解决方法是在方法调用Key2 / Order2Key3 / Order3参数交换。 奇怪的是,它已经奏效了。