VBA将空白单元格排序到底部

时间:2017-08-02 11:35:26

标签: vba sorting blank-line

我想从excel列中重新发送一些重复的值,但在此之前我需要对其进行排序,以便它不会Remowe我想要的任何contatn。我得到的问题是空白单元格,我希望它们能够被移除但是它们会保持在顶部的任何分类。这是我的排序代码,任何想法?

ABC.Range("A1", "AF" & lngLastRow).Sort key1:=ABC.Range("A1:A" & lngLastRow), _
   order1:=xlDescending, key2:=ABC.Range("P1:P" & lngLastRow), _
   order2:=xlDescending, MatchCase:=False, Orientation:=xlTopToBottom, Header:=xlNo

我需要一个解决方案,我可以使用第二个键将它们(空白)分类到底部。我不需要一个解决方案,我删除它们,因为一些重复项在此列中只有空白值,如果它们没有任何替代,我仍然需要保留它们。

3 个答案:

答案 0 :(得分:0)

以下是我的工作方式:检查指定列中是否有空白,如果是,请将整行移至底部并将其删除(以便整个表向上移动一行)。重复,直到指定列顶部的非空单元格为止。

Sub MoveBlanksToBottom()
'you have to specify size of your table here
Dim rows, columns, columnsWithBlanks As Long
rows = 10
columns = 4
columnWithBlanks = 1
'we will loop until the first row in specified column will be non empty
Do While IsEmpty(Cells(1, columnWithBlanks))
    'copy entire row to the bottom
    For i = 1 To columns
        Cells(rows + 1, i).Value = Cells(1, i).Value
    Next
    'delete the row
    Cells(1, 1).EntireRow.Delete
Loop

End Sub

在对表格进行排序后,您应该使用此Sub

您可以将表的大小作为参数传递,以使其更灵活:

Sub MoveBlanksToBottom()
'you have to specify size of your table here
Dim rows, columns, columnsWithBlanks As Long
rows = 10
columns = 4
columnWithBlanks = 1

应替换为

Sub MoveBlanksToBottom(rows As Long, columns As Long, columnsWithBlanks As Long)

然后你可以这样称呼它:Call MoveBlanksToBottom(10, 4, 1)

答案 1 :(得分:0)

这个选项应该对空白单元格进行排序: DataOption:=xlSortTextAsNumbers

答案 2 :(得分:-1)

这涉及删除空白单元格。根据需要更改工作表名称和列参数:

Dim i As Long
Dim n As Long

n = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

With Sheets("Sheet1")
    For i = 1 To n
        If .Cells(i, 1).Value = "" Then
            .Cells(i, 1).EntireRow.Delete
        End If
    Next i
End With