尝试对三个不同值进行数据排序时出现运行时错误1004 [英] Run time error 1004 when trying to sort data on three different values

查看:54
本文介绍了尝试对三个不同值进行数据排序时出现运行时错误1004的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

单步执行代码时,排序的.apply参数出现错误1004.

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

运行时错误"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.

任何人都可以提出原因或解决此问题的方法吗?

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.

亲切的问候

Pete

推荐答案

对我来说,就像您选择了A:N进行排序一样,但是正在对R和S以及D的值进行排序.在LastCell上放一块手表-如果我是对的,那还远远不够,您无法真正对想要的东西进行排序.

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.

这篇关于尝试对三个不同值进行数据排序时出现运行时错误1004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆