无法sorting由于错误1084sorting引用无效

我是VBA领域的初学者,如果有人能帮助我,我将不胜感激。

基本上r1c1是用于select行和列的variables,这有助于我select定义的范围。

但是,当我尝试对此选定范围进行sorting时,popuperror 1084 。 我已经尝试了很多版本的sorting方法,但问题仍然存在。 这是我的代码

 With ActiveWorkbook.Worksheets("Main").Sort .SortFields.Clear .SortFields.Add Key:=Range(Cells(8, 1), Cells(r1 + 5000, c1 + 8)), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange Range(Cells(8, 1), Cells(r1 + 5000, c1 + 8)) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

问题在于这两条线

 Key:=Range(Cells(8, 1), Cells(r1 + 5000, c1 + 8)) 

 .SetRange Range(Cells(8, 1), Cells(r1 + 5000, c1 + 8)) 

您的范围和单元格对象不完全合格。 试试这个( 未经testing

同样在.SortFields.AddKey:应只引用单个列。 例如,如果您正在对第1列进行sorting,则将Key:=Range(Cells(8, 1), Cells(r1 + 5000, c1 + 8))更改为Key:=Range(Cells(8, 1), Cells(r1 + 5000, 1))

 Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Main") With ws.Sort .SortFields.Clear .SortFields.Add Key:=ws.Range(ws.Cells(8, 1), ws.Cells(r1 + 5000, 1)), _ SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal .SetRange ws.Range(ws.Cells(8, 1), ws.Cells(r1 + 5000, c1 + 8)) .Header = xlYes .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

不知何故,我仍然喜欢Excel 2003的sorting方式。 例如

 ws.Somerng.Sort Key1:=ws.Range("A2"), Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal