Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Excel 范围已排序,但我得到了";范围类的排序方法失败“;错误_Excel_Sorting_Vba - Fatal编程技术网

Excel 范围已排序,但我得到了";范围类的排序方法失败“;错误

Excel 范围已排序,但我得到了";范围类的排序方法失败“;错误,excel,sorting,vba,Excel,Sorting,Vba,在工作表“all”中,我有A列到AC列的数据范围。每个范围的开头由A列中包含“#”的字符串定义。下面的例程在每行上方插入一个带“#”的空行,并根据H列中的值按降序对每个范围进行排序。最后,删除所有插入的空行 但是,当我运行代码时,排序工作正常,在 请帮助改进以下代码,以避免产生错误 Sub SortRanges() Dim LR As Long Dim myCount As Long Dim r As Long Dim i As Long Dim Rng As Range Applicati

在工作表“all”中,我有A列到AC列的数据范围。每个范围的开头由A列中包含“#”的字符串定义。下面的例程在每行上方插入一个带“#”的空行,并根据H列中的值按降序对每个范围进行排序。最后,删除所有插入的空行

但是,当我运行代码时,排序工作正常,在

请帮助改进以下代码,以避免产生错误

Sub SortRanges()

Dim LR As Long
Dim myCount As Long
Dim r As Long
Dim i As Long
Dim Rng As Range

Application.ScreenUpdating = False


With Sheets("all")
LR = .Range("A" & .Rows.Count).End(xlUp).Row
Set Rng = .Range("A2:A" & LR)
With Rng

For i = LR To 1 Step -1

If InStr(Rng(i).Value, "#") > 0 Then
    Rng(i).Offset(0, 0).EntireRow.Insert

End If
Next
End With

.Range("A3").CurrentRegion.Offset(1, 0).Sort Key1:=Range("H4"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

Set Rng = .Range("A3").CurrentRegion

r = Rng(Rng.Count).Row
LR = .Range("A" & .Rows.Count).End(xlUp).Row
myCount = Application.WorksheetFunction.CountBlank(.Range("A1:A" & LR))

For i = 1 To myCount

.Range("A" & r).Offset(2, 0).CurrentRegion.Offset(1, 0).Sort Key1:=.Range("H4"), Order1:=xlDescending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Set Rng = .Range("A" & r).Offset(2, 0).CurrentRegion

r = Rng(Rng.Count).Row

Next i

LR = .Range("A" & .Rows.Count).End(xlUp).Row

.Range("A1:A" & LR).AutoFilter Field:=1, Criteria1:="="

.Range("A2:AC" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
.AutoFilterMode = False
End With

Application.ScreenUpdating = True

End Sub

插入空白行不是排序所必需的,只需定义排序范围即可。下面的示例确定每个部分的范围并对其进行排序,直至第一个空单元格

已编辑:添加了逻辑以对所有工作表进行排序,指定为跳过的工作表除外

Option Explicit

Sub test()
    '--- sort each section by column H
    Dim sh As Worksheet
    Dim section As Range
    Dim sortCol As Range
    Dim startRow As Long
    Dim numRows As Long
    Dim skipSheets As String

    skipSheets = "Sheet1,After Sort,Unsorted"

    For Each sh In ThisWorkbook.Sheets
        Debug.Print "------------------------------------------------------"
        Debug.Print "Checking sheet " & sh.Name & "... ";
        If InStr(1, skipSheets, sh.Name, vbTextCompare) = 0 Then
            Debug.Print "sorting!"
            Set section = sh.Range("A2:AC2")
            Do While True
                startRow = section.Row
                numRows = RowsInSection(startRow)
                Debug.Print vbTab & "section starting at row " & Format(startRow, "@@@") & _
                                    " with " & Format(numRows, "@@@") & " to sort: ";
                If numRows > 1 Then
                    '--- the section to sort does NOT include the first row
                    '    (with the "#" as the first character in column A)
                    '    so shift the section range down by one row and expand
                    '    to include all the rows
                    Set section = section.Offset(1, 0)
                    Set section = section.Resize(numRows - 1, section.Columns.Count)
                    '--- now set the column range on which to sort
                    Set sortCol = sh.Range("H" & startRow + 1, "H" & startRow + numRows - 1)
                    Debug.Print "range: " & section.Address;
                    Debug.Print " sort key: " & sortCol.Address
                    '--- finally sort it!
                    section.Sort Key1:=sortCol, Order1:=xlDescending, Header:=xlGuess, _
                                 OrderCustom:=1, MatchCase:=False, _
                                 Orientation:=xlTopToBottom
                End If
                Set section = section.Offset(numRows - 1, 0)
                If IsEmpty(section.Cells(1, 1)) Then
                    Exit Do
                End If
            Loop
        Else
            Debug.Print "skipped."
        End If
    Next sh
End Sub

Function RowsInSection(startRow As Long) As Long
    Dim i As Long
    i = startRow + 1
    Do While True
        If IsEmpty(Cells(i, 1)) Or Left(Cells(i, 1).Value, 1) = "#" Then
            Exit Do
        Else
            i = i + 1
        End If
    Loop
    RowsInSection = i - startRow
End Function

插入空白行不是排序所必需的,只需定义排序范围即可。下面的示例确定每个部分的范围并对其进行排序,直至第一个空单元格

已编辑:添加了逻辑以对所有工作表进行排序,指定为跳过的工作表除外

Option Explicit

Sub test()
    '--- sort each section by column H
    Dim sh As Worksheet
    Dim section As Range
    Dim sortCol As Range
    Dim startRow As Long
    Dim numRows As Long
    Dim skipSheets As String

    skipSheets = "Sheet1,After Sort,Unsorted"

    For Each sh In ThisWorkbook.Sheets
        Debug.Print "------------------------------------------------------"
        Debug.Print "Checking sheet " & sh.Name & "... ";
        If InStr(1, skipSheets, sh.Name, vbTextCompare) = 0 Then
            Debug.Print "sorting!"
            Set section = sh.Range("A2:AC2")
            Do While True
                startRow = section.Row
                numRows = RowsInSection(startRow)
                Debug.Print vbTab & "section starting at row " & Format(startRow, "@@@") & _
                                    " with " & Format(numRows, "@@@") & " to sort: ";
                If numRows > 1 Then
                    '--- the section to sort does NOT include the first row
                    '    (with the "#" as the first character in column A)
                    '    so shift the section range down by one row and expand
                    '    to include all the rows
                    Set section = section.Offset(1, 0)
                    Set section = section.Resize(numRows - 1, section.Columns.Count)
                    '--- now set the column range on which to sort
                    Set sortCol = sh.Range("H" & startRow + 1, "H" & startRow + numRows - 1)
                    Debug.Print "range: " & section.Address;
                    Debug.Print " sort key: " & sortCol.Address
                    '--- finally sort it!
                    section.Sort Key1:=sortCol, Order1:=xlDescending, Header:=xlGuess, _
                                 OrderCustom:=1, MatchCase:=False, _
                                 Orientation:=xlTopToBottom
                End If
                Set section = section.Offset(numRows - 1, 0)
                If IsEmpty(section.Cells(1, 1)) Then
                    Exit Do
                End If
            Loop
        Else
            Debug.Print "skipped."
        End If
    Next sh
End Sub

Function RowsInSection(startRow As Long) As Long
    Dim i As Long
    i = startRow + 1
    Do While True
        If IsEmpty(Cells(i, 1)) Or Left(Cells(i, 1).Value, 1) = "#" Then
            Exit Do
        Else
            i = i + 1
        End If
    Loop
    RowsInSection = i - startRow
End Function


你用的是什么版本的excel,2003年,2010年?我有excel 2010。感谢当前可见的表格“全部”?@MatthewD我确实有标题行a。这可能是问题所在吗?如果是,我该怎么办?谢谢,页眉不重要。我不确定Key1:=Range(“H4”)是否可以尝试Key1:=Range(“H:H”)您使用的是什么版本的excel,2003、2010?我有excel 2010。感谢当前可见的表格“全部”?@MatthewD我确实有标题行a。这可能是问题所在吗?如果是,我该怎么办?谢谢,页眉不重要。我不确定Key1:=Range(“H4”)是否可以尝试Key1:=Range(“H:H”)谢谢Peter,它工作得很好,因为H列的每个范围之间都有一个空白单元格。我正在尝试将此代码应用于工作簿中的所有工作表,但某些工作表除外,并且有问题,即使所有的工作表都有相同的格式,但排序并不总是正确的。我正在为此工作簿中的每个sh使用for each loop
。如果(sh.name“Access”)和(sh.name“Report_1”)和(sh.name“Report_2”)和(sh.name“IGVLinks”),则使用sh Set section=sh.Range(“A1:AC1”)执行,而为True。。。循环
有什么建议吗?谢谢您根据您评论中的代码片段,您将
与sh一起使用
,然后使用
Set section=sh.Range…
。如果您将
与sh一起使用
则可以省略
sh。
我已尝试删除sh。结果与以前相同:前两张表已排序,但后两张表未排序。但是,当我在每张图纸上一次运行一个宏时,效果非常好。请更新您的答案,并说明如何正确地将代码应用于同一工作簿中的多个工作表,以及如何省略指定的工作表?我非常感谢你的帮助。感谢您上述编辑允许对所有工作表进行排序,跳过列表中注明的除外。感谢Peter,鉴于H列中每个范围之间都有一个空白单元格,该操作非常有效。我正在尝试将此代码应用于工作簿中的所有工作表,但某些工作表除外,并且存在问题,即使所有的工作表都有相同的格式,但排序并不总是正确的。我正在为此工作簿中的每个sh使用for each loop
。如果(sh.name“Access”)和(sh.name“Report_1”)和(sh.name“Report_2”)和(sh.name“IGVLinks”),则使用sh Set section=sh.Range(“A1:AC1”)执行,而为True。。。循环
有什么建议吗?谢谢您根据您评论中的代码片段,您将
与sh一起使用
,然后使用
Set section=sh.Range…
。如果您将
与sh一起使用
则可以省略
sh。
我已尝试删除sh。结果与以前相同:前两张表已排序,但后两张表未排序。但是,当我在每张图纸上一次运行一个宏时,效果非常好。请更新您的答案,并说明如何正确地将代码应用于同一工作簿中的多个工作表,以及如何省略指定的工作表?我非常感谢你的帮助。谢谢以上编辑允许对所有图纸进行排序,跳过列表中注明的除外。