VBA对象不支持属性或方法

时间:2015-03-26 20:44:53

标签: excel vba

我正在尝试根据几个标准对一组工作表进行排序,但表格的大小和列位置在每个工作表中都有所不同。

这是一个我遇到错误的排序程序。

这是一行,我采用了录制宏的代码,并将原始范围更改为以下内容,并返回错误。

.SetRange .Range(.Cells(4, 2), .Cells(lastRow, lastColumn))

对象不支持属性或方法

有人可以帮助我吗?谢谢!

Sub SortAll()
    Dim RngAll As Range
    Dim lastRow As Long
    Dim lastColumn As Long
    Dim reportPeriod As String
    Dim aCell As Range
    Dim col As Long, lRow As Long
    Dim colName As String


        reportPeriod = Sheets("Ranking Report").Range("C36").Value


        With Worksheets(reportPeriod)


        lastColumn = .Cells(4, Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set aCell = .Range("B4:BT4").Find(What:="Penetration Overall", LookIn:=xlValues, LookAt:=xlWhole, _
                    MatchCase:=False, SearchFormat:=False)

            If Not aCell Is Nothing Then
            col = aCell.Column
            colName = Split(.Cells(, col).Address, "$")(1)

            lRow = .Range(colName & .Rows.Count).End(xlUp).Row

            '~~> This is your range
            Set RngAll = .Range(colName & "5:" & colName & lRow)

            Debug.Print RngAll.Address
        '~~> If not found
        Else
            MsgBox "Range Not Found"
        End If


        .Sort.SortFields.Clear
        .Sort.SortFields.Add Key:=RngAll, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal

        End With

        With ActiveWorkbook.Worksheets(reportPeriod).Sort
            .SetRange .Range(.Cells(4, 2), .Cells(lastRow, lastColumn))
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

2 个答案:

答案 0 :(得分:0)

我想通了如果我事先定义了排序区域,然后在排序公式中引用它,它就可以了。

像这样,我仍然不明白为什么会这样。如果有人能解释,我非常感谢!

Sub SortAll()
    Dim RngAll As Range
    Dim SortA As Range
    Dim lastRow As Long
    Dim lastColumn As Long
    Dim reportPeriod As String
    Dim aCell As Range
    Dim col As Long, lRow As Long
    Dim colName As String


        reportPeriod = Sheets("Ranking Report").Range("C36").Value


        With Worksheets(reportPeriod)


                lastColumn = .Cells(4, Columns.Count).End(xlToLeft).Column
                lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
                Set SortA = .Range(.Cells(4, 2), .Cells(lastRow, lastColumn))
                Set aCell = .Range("B4:BT4").Find(What:="Penetration Overall", LookIn:=xlValues, LookAt:=xlWhole, _
                            MatchCase:=False, SearchFormat:=False)

                    If Not aCell Is Nothing Then
                        col = aCell.Column
                        colName = Split(.Cells(, col).Address, "$")(1)

                        lRow = .Range(colName & .Rows.Count).End(xlUp).Row

                        'This is your range
                        Set RngAll = .Range(colName & "5:" & colName & lRow)

                        Debug.Print RngAll.Address
                        'If not found
                    Else
                        MsgBox "Range Not Found"
                    End If


                .Sort.SortFields.Clear
                .Sort.SortFields.Add Key:=RngAll, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
                xlSortNormal

        End With

        With ActiveWorkbook.Worksheets(reportPeriod).Sort
            .SetRange SortA
            .header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

答案 1 :(得分:0)

如果您有列索引号,则无需将其重新编写为xlA1范围地址。

Sub SortAll()
    Dim lr As Long, lc As Long
    Dim reportPeriod As String
    Dim aCell As Range
    Dim lCol As Long, lRow As Long

    reportPeriod = Sheets("Ranking Report").Range("C36").Value
    With Worksheets(reportPeriod)
        lc = .Cells(4, Columns.Count).End(xlToLeft).Column
        lr = .Cells(.Rows.Count, "B").End(xlUp).Row

        Set aCell = .Rows(4).Find(What:="Penetration Overall", LookIn:=xlValues, _
                                      LookAt:=xlWhole, MatchCase:=False)

        If Not aCell Is Nothing Then
            lCol = aCell.Column
            lRow = .Cells(Rows.Count, lCol).End(xlUp).Row

            With .Cells(4, 1).Resize(lRow, lc)
                .Cells.Sort Key1:=.Columns(lCol), Order1:=xlDescending, _
                        Orientation:=xlTopToBottom, Header:=xlYes
            End With
        Else
            MsgBox "Range Not Found"
        End If
    End With
End Sub