查找最后一列并在最后一列上排序

希瑟

我需要Excel来检测我拥有的最后一列并对该列进行排序。我有一个宏,每次使用它都会生成一个新列,因此我不能使用常量。

Sub sortyness() 
Dim sortdata(A1 & ":", Cells(LastRow, LastColumn)) As Range

ActiveWorkbook.Worksheets("Compiled").Sort.SortFields.Clear    
ActiveWorkbook.Worksheets("Compiled").Sort.SortFields.Add _    
    Key:=Range(Sorton), Sorton:=xlSortOnValues, Order:=xlAscending, _    
    DataOption:=xlSortNormal

With ActiveWorkbook.Worksheets("Compiled").Sort    
    .SetRange Range(sortdata)    
    .Header = xlYes    
    .MatchCase = False    
    .Orientation = xlTopToBottom    
    .SortMethod = xlPinYin    
    .Apply    
End With

End Sub

这是工作表的屏幕截图: 片

我在按最后一列进行排序时遇到麻烦。我是否可以通过在第1行中查找第一个没有数据的单元格来定义该列,然后以此为基础进行排序?我应该如何修改我的VBA?

谢谢。

我不知道如何编辑此东西以使其不会显示为重复项,但显然不是重复项。与查找最后一列相比,Mine更关心在最后一列上运行宏。

用户名

实际上,vba排序操作所需的代码比从录制中获取的代码少得多。

Dim sortdata As Range, LastRow as long, LastColumn as long

With ActiveWorkbook.Worksheets("Compiled")
    LastRow = .cells(.rows.count, "A").end(xlup).row
    LastColumn = .cells(1, .columns.count).end(xltoleft).column
    with .range(.cells(1, 1), .Cells(LastRow, LastColumn))
         .Cells.Sort Key1:=.Columns(.columns.count), Order1:=xlAscending, _
                     Orientation:=xlTopToBottom, Header:=xlyes
    end with
end with

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章