对带有空格的列进行排序

Sort column with empty spaces

我正在尝试对 excel 中各数据条目之间有空格的列进行排序。

上下文:最初,我有一个删除行之间空格的宏,但是,在向社区询问为什么宏需要这么长时间才能完成 运行 之后,我是 告知该宏实际上是在删除整个列中的每个银行单元格。

所以我创建了以下代码来对数据进行排序(数字出现的顺序无关紧要)。

虽然下面的代码运行良好,但我正在尝试一种编写它的方法,这样它就不会引用固定数量的单元格,因为单元格列表会有所不同。

原码

      Columns("E:E").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range( _
        "E1:E3121"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("E1:E3121")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

所以我写了下面的代码,以便它可以使用当前区域自动检测所有单元格,但是,因为 CurrentRegion 只能找到彼此相邻的单元格,而在我的专栏中有数千个单元格被分隔多个空单元格之一,下面的代码仅对第一个相邻的单元格块进行排序。

新代码

      Columns("E:E").Select
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("E1").CurrentRegion, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("E1").CurrentRegion
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply

有没有一种方法可以编写上面的代码,以便检测列中的最后一个单元格并对其进行排序?

谢谢

对列进行排序

Sub SortColumn()
    With ThisWorkbook.Worksheets("Sheet1").Columns("E")
        Dim lCell As Range
        Set lCell = .Find("*", , xlFormulas, , , xlPrevious)
        If lCell Is Nothing Then Exit Sub ' empty column
        With .Resize(lCell.Row)
            .Sort .Cells, xlAscending, , , , , , xlNo
        End With
    End With
End Sub

编辑

Sub SortColumnVariables()
    
    Dim crg As Range ' Whole Column; `E1:E1048576`
    Set crg = ThisWorkbook.Worksheets("Sheet1").Columns("E")
    
    Dim lCell As Range ' Last Cell
    ' Starting from the last column cell ('After:=crg.Cells(1)' i.e. 'E1048576'),
    ' find the first non-empty ('LookIn:=xlFormulas') cell looking
    ' upwards ('SearchDirection:=xlPrevious').
    Set lCell = crg.Find("*", crg.Cells(1), xlFormulas, , , xlPrevious)
    ' You can also use the argument names when the order is not important:
    'Set lCell = crg.Find(What:="*", After:=crg.Cells(1), _
        LookIn:=xlFormulas, SearchDirection:=xlPrevious)
    
    If lCell Is Nothing Then Exit Sub ' empty column; no non-empty cell found
        
    ' Reference the range from the first ('E1')
    ' to the last (non-empty) cell ('lCell').
    Set crg = crg.Resize(lCell.Row)
    ' Or more obviously:
    'Set crg = ThisWorkbook.Worksheets("Sheet1").Range(crg.Cells(1), lCell)
        
        
    ' Try this:
    ' Put the cursor to the left of a comma, delete the comma and add it again.
    ' Now you will see the intelli-sense showing you loads of 'Sort' parameters.
    ' i.e. `xlNo` refers to the headers.
    crg.Sort crg, xlAscending, , , , , , xlNo
    ' You can also use the argument names when the order is not important:
    'crg.Sort Key1:=crg, Order1:=xlAscending, Header:=xlNo

End Sub