Excel VBA运行时错误1004 [英] Excel VBA Run-Time Error 1004

查看:1116
本文介绍了Excel VBA运行时错误1004的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


运行时错误'1004':排序参考无效,请确保它在您要排序的数据中,而第一个排序方式框不相同或为空。


这是代码

 '对活动行排序
使用ActiveWorkbook.Worksheets(Product Backlog ).Sort
.SetRange范围(A4:F51)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
。应用
结束


解决方案

按框排序是空白的,这是你的问题。我从来没有使用像你这样做的Sort对象,但是我可以看到你没有定义一个键或一个要排序的范围,只是要排序的范围。应该定义一个关键字,例如Range(A4)或某些东西。我看着它,它应该有.sortfields.add(范围),如:

 '排序活动行

使用ActiveWorkbook.Worksheets(Product Backlog)排序
.SetRange范围(A4:F51)
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin

.SortFields.Add Key:= Range(A4:F51)。列(1),SortOn:= xlSortOnValues, _
订单:= xlDescending,DataOption:= xlSortNormal

。应用
结束

我使用Sort功能如下:

  ActiveWorkbook.Worksheets(Product Backlog) .Range(A4:F51)排序_ 
Key1:= ActiveWorkbook.Worksheets(Product Backlog)。范围(A4:F51)。列(1),_
标题: = xlYes,_
方向:= xlSortColumns,_
MatchCase:= False,_
SortMethod:= xlPinYin


I am using VBA for Excel 2010 and randomly receiving the following error:

Run-time error '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."

This is the code

'Sort the active rows
With ActiveWorkbook.Worksheets("Product Backlog").Sort
    .SetRange Range("A4:F51")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

解决方案

The sort by box is blank, that is your problem. I never have used a Sort object like your doing, but I can see that you have not defined a key, or a range to sort by, just the range to be sorted. A key should be defined such as Range("A4") or something. I looked it up, it should have .sortfields.add (range) in it, such as:

'Sort the active rows

    With ActiveWorkbook.Worksheets("Product Backlog").Sort
        .SetRange Range("A4:F51")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin

        .SortFields.Add Key:=Range("A4:F51").Columns(1), SortOn:=xlSortOnValues, _
         Order:=xlDescending, DataOption:=xlSortNormal

        .Apply
    End With

I use the Sort function as follows:

ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Sort _
    Key1:= ActiveWorkbook.Worksheets("Product Backlog").Range("A4:F51").Columns(1), _
    Header:= xlYes, _
    Orientation:=xlSortColumns, _
    MatchCase:=False, _
    SortMethod:=xlPinYin

这篇关于Excel VBA运行时错误1004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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