VBA对象不支持属性或方法

我试图根据几个标准对一组工作表进行sorting,但是表的大小和列的位置在每个工作表中都不相同。

这是我遇到错误的sorting过程。

这是行,我通过录制macros的代码,并将原始范围更改为以下,并返回一个错误。

.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 

如果有列索引号,则不需要将其重新编译为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 

我想,如果我提前定义了sorting区域,然后在sorting公式中引用它,它的工作原理。

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

 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