从A到Z列中的单元格进行sorting

代码来自稍微编辑的macros。 我试图删除“乱码”,但它不工作。 它的目的是将数据从列A到Zsorting。

Dim InSheet As Worksheet Set InSheet = ThisWorkbook.Worksheets("A to Z") Dim LastRow as Integer LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row InSheet.Select Columns("BF:BF").Select InSheet.Sort.SortFields.Clear InSheet.Sort.SortFields.Add Key:=Range( _ "BF1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With InSheet.Sort .SetRange Range("A1:BF" & LastRow) .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

所以我试了这个,它不工作:

  Dim InSheet As Worksheet Set InSheet = ThisWorkbook.Worksheets("A to Z") Dim LastRow as Integer LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row InSheet.Columns("BF:BF") InSheet.Sort.SortFields.Clear InSheet.Sort.SortFields.Add Key:=Range( _ "BF1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _ xlSortNormal With InSheet.Sort .SetRange Range("A1:BF" & LastRow) .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With 

任何ides? 我想真的避免select其他的东西,因为它是巨大的性能下降。

非常感谢

你可以直接用一个键来sorting范围,而不用你剩下的代码。

 Range("A1:BF" & LastRow).SORT Key1:=Range("BF1"), Order1:=xlAscending_ Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _ xlTopToBottom, DataOption1:=xlSortNormal 

所以我用了你们两个提供的build议,最后这个工作很好:

 Dim InSheet As Worksheet Set InSheet = ThisWorkbook.Worksheets("A to Z") Dim LastRow As Integer LastRow = InSheet.Cells(Rows.Count, 58).End(xlUp).Row With InSheet.Sort ' sort data from A to Z .SetRange InSheet.Range("A1:BF" & LastRow) .Header = xlGuess .MatchCase = False .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply End With