vba 尝试对三个不同值的数据进行排序时出现运行时错误 1004

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15026539/
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-11 19:45:09  来源:igfitidea点击:

Run time error 1004 when trying to sort data on three different values

excel-vbaruntime-errorexcel-2010vbaexcel

提问by Petethefeet

I've read at least fifteen different articles on this and tried a number of different possible solutions. I am trying to use the VBA code below to sort a worksheet of variable size by three different levels.

我已经阅读了至少十五篇不同的文章,并尝试了许多不同的可能解决方案。我正在尝试使用下面的 VBA 代码按三个不同级别对可变大小的工作表进行排序。

When I step through the code I get an error 1004 at the .apply argument of the sort.

当我逐步执行代码时,我在 .apply 类型的参数中收到错误 1004。

Run-time error '1004':

运行时错误“1004”:

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

排序引用不是 vald。确保它在您要排序的数据内,并且第一个排序依据框不相同或不为空白。

Can anyone suggest a reason why or a solution to this problem?

任何人都可以提出这个问题的原因或解决方案吗?

Sub Subbing()

Dim LastCell as String

Range("A2").End(xlDown).Select

LastCell = Selection.Offset(0, 13).Address
Worksheets("Sheet1").Activate

With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SortFields.Clear
        .SetRange Range("A2", LastCell)
        .SortFields.Add Key:=ActiveWorkbook.Worksheets("Sheet1").Range("R2"), _ SortOn:=xlSortOnValues, Order:=xlAscending
        .SortFields.Add Key:=ActiveWorkbook.Worksheets("Sheet1").Range("S2"), _ SortOn:=xlSortOnValues, Order:=xlAscending
        .SortFields.Add Key:=ActiveWorkbook.Worksheets("Sheet1").Range("D2"), _ SortOn:=xlSortOnValues, Order:=xlAscending
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

End With
End Sub

I look forward to your responses. I know this is a common question but I have tried all of the widely suggested methods of resolving my issue and to no avail.

我期待着您的回复。我知道这是一个常见问题,但我已经尝试了所有广泛建议的方法来解决我的问题,但无济于事。

Kind Regards

亲切的问候

Pete

皮特

回答by Joe

Looks to me like you selected A:N to sort, but are sorting on the values of R and S as well as D. Run it through Step mode and put a watch on LastCell - if I'm right, that won't be far enough over for you to actually sort what you want.

在我看来,您选择了 A:N 进行排序,但是正在对 R 和 S 以及 D 的值进行排序。通过 Step 模式运行它并在 LastCell 上放置一个手表 - 如果我是对的,那不会远远足以让您真正对您想要的东西进行排序。

回答by John Bustos

... Your criteria range is outside of the sort area - You can't do this.

... 您的标准范围在排序区域之外 - 您不能这样做。

What I mean is that your sort range will be between Column A and Column N (13 columns after A), but your criteria are in Columns R & S.

我的意思是,您的排序范围将在 A 列和 N 列之间(A 后 13 列),但您的条件在 R & S 列中。

If you extend your sort range, all will work.

如果您扩展排序范围,一切都会奏效。

Hope this fixes everything.

希望这能解决所有问题。