Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
运行时错误1004排序函数VBA_Vba_Excel - Fatal编程技术网

运行时错误1004排序函数VBA

运行时错误1004排序函数VBA,vba,excel,Vba,Excel,当我运行这段代码时,大约有一半的时间我会遇到错误1004,我根本不知道为什么: Dim ranged As Range Set ranged = Range("AJ2") Set ranged = Range(ranged, ranged.End(xlDown)) Sheets(i).Select ActiveWorkbook.Worksheets(i).Sort.SortFields.Add Key:=ranged, _ SortOn:=xlSortOnValues, Order:

当我运行这段代码时,大约有一半的时间我会遇到错误1004,我根本不知道为什么:

Dim ranged As Range
Set ranged = Range("AJ2")
Set ranged = Range(ranged, ranged.End(xlDown))


Sheets(i).Select
ActiveWorkbook.Worksheets(i).Sort.SortFields.Add Key:=ranged, _
    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(i).Sort
    .SetRange ranged
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply

End With
代码在一系列工作表上运行,AJ列中的范围在每张工作表上的大小不同,因此需要在开始时定义范围。其目的是改变射程的顺序。有人能帮忙解决这里的错误吗

  • 在大多数情况下,您不需要选择工作表。你可能想看看

  • 不要使用
    xlDown
    构建您的范围。使用
    xlUp
    查找包含数据的最后一行。你可能想看看

  • 结合以上两者,您的代码可以如下所示。请试一试。(未经测试

    经过测试的版本

    Sub Sample()
        Dim ranged As Range
        Dim lRow As Long, i As Long
    
        For i = 1 To ThisWorkbook.Sheets.Count
            '
            '~~> Rest of the code
            '
    
            With ThisWorkbook.Sheets(i)
                '~~> Find Last row in Col AJ which has data
                lRow = .Range("AJ" & .Rows.Count).End(xlUp).Row
    
                '~~> Construct your range
                Set ranged = .Range("AJ2:AJ" & lRow)
    
                '~~> Sort
                .Sort.SortFields.Add Key:=ranged, _
                                     SortOn:=xlSortOnValues, _
                                     Order:=xlDescending, _
                                     DataOption:=xlSortNormal
                With .Sort
                    .SetRange ranged
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
            End With
    
            '
            '~~> Rest of the code
            '
        Next i
    End Sub
    

    谢谢你的帮助。我将这段代码完全复制到我的代码中,但它仍然会出现“运行时错误1004:排序引用无效。请确保它位于要排序的数据中,并且第一个排序依据框不相同或为空。”真正奇怪的是,当我仅通过使用此工作簿进行更改在一张工作表上运行它时。工作表(I)使用ThisWorkbook.Sheets(“Sheetname”)可以很好地工作,但是当我将它放入上面的结构中,按照您的代码运行多个时,它就不工作了。有什么想法吗?
    Sub Sample()
        Dim ranged As Range
        Dim lRow As Long, i As Long
    
        For i = 1 To ThisWorkbook.Sheets.Count
            '
            '~~> Rest of the code
            '
    
            With ThisWorkbook.Sheets(i)
                '~~> Find Last row in Col AJ which has data
                lRow = .Range("AJ" & .Rows.Count).End(xlUp).Row
    
                '~~> Construct your range
                Set ranged = .Range("AJ2:AJ" & lRow)
    
                '~~> Sort
                .Sort.SortFields.Add Key:=ranged, _
                                     SortOn:=xlSortOnValues, _
                                     Order:=xlDescending, _
                                     DataOption:=xlSortNormal
                With .Sort
                    .SetRange ranged
                    .Header = xlNo
                    .MatchCase = False
                    .Orientation = xlTopToBottom
                    .SortMethod = xlPinYin
                    .Apply
                End With
            End With
    
            '
            '~~> Rest of the code
            '
        Next i
    End Sub