这种排序语法有什么问题,给我“排序引用无效”?

问题描述 投票:0回答:1

我的宏组合了两张纸并清理它们以进行进一步处理。我在排序语句中不断收到“排序引用无效”错误。我这里尝试了很多方法都没有效果。有人可以建议更正吗?我是一个笨手笨脚的程序员,我知道还有其他方法来处理数据集,但是:

'iterate worksheet to delete column A - size sorter / delete all rows without a quantity / sort columns
 Dim rng As Range
 Dim chkcell As String
 Dim lastrow As Long

For Each MmasterWS In MmasterWB.Worksheets
    MmasterWS.Range("A:A").Delete ' delete column A - size sorter
    
    lastrow = MmasterWS.Range("B" & MmasterWS.Rows.Count).End(xlUp).Row
    
    MmasterWS.Range("A4:O" & lastrow).Interior.ColorIndex = 2 'change all rows to white
    
    For Each rng In MmasterWS.Range("B3:O" & lastrow) 'iterates data range to find empty rows
        
        If lastrow = 3 Then 'sorts sheet by vendor first then harm code. iterates to next sheet in workbook
            
            lastrow = MmasterWS.Range("B" & MmasterWS.Rows.Count).End(xlUp).Row
            
            MmasterWS.Range("A3:O" & lastrow).Sort key1:=Columns(3), Order1:=xlAscending, Key2:=Columns(14), Order2:=xlAscending, Header:=xlYes
            
            GoTo skip2
        
        Else
            
            chkcell = MmasterWS.Range("A" & lastrow).Value 
                If chkcell = "" Then
                    MmasterWS.Range("A" & lastrow).EntireRow.Delete 'deletes empty rows
                End If
                lastrow = lastrow - 1 'moves up one row
        
        End If
     Next

skip2:
Next

在 Stackoverflow 上尝试了很多建议。任何帮助表示赞赏。 凯文

excel vba sorting
1个回答
0
投票

我解决了这个问题。谢谢尼克的语法修正。这是我修复它的方法:

lastrow = MmasterWS.Range("B" & MmasterWS.Rows.Count).End(xlUp).Row
        With MmasterWS.Sort
            .SortFields.Clear
            .SortFields.Add2 Key:=MmasterWS.Range("N4:N" & lastrow), Order:=xlAscending
            .SortFields.Add2 Key:=MmasterWS.Range("C4:C" & lastrow), Order:=xlAscending
            .SetRange MmasterWS.Range("A3:N" & lastrow)
            .Header = xlYes
            .Apply
        End With
© www.soinside.com 2019 - 2024. All rights reserved.