尝试创建动态排序宏,在第一列上起作用,然后在尝试在新列上运行时出现错误(尝试在不更改其他列顺序的情况下对A-Z列进行排序)。
Sub SortCell()
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set sht = ActiveSheet
Set StartCell = ActiveCell
ActiveCell.Columns("A:A").EntireColumn.Select
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:= _
ActiveCell, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveSheet.Sort
.SetRange ActiveCell.Range(StartCell, sht.Cells(LastRow, LastColumn))
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
MsgBox "Action complete.", vbInformation
End Sub
答案 0 :(得分:1)
您正在尝试使用录音使用的排序代码版本。这非常冗长,并且“记住”先前操作中的参数值。有一种更简洁的语法可以有效地发挥作用。
dim c as long
with activesheet
for c=1 to .cells(1, .columns.count).end(xltoleft).column
if .cells(.rows.count, c).end(xlup).row >1 then
with .columns(c).Cells
.Sort Key1:=.cells(1), Order1:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlNo
end with
end if
next c
end with