2017-08-02 192 views
0

我想從excel列中重新獲取一些重複的值,但在此之前我需要對其進行排序,以便它不會Remowe出現任何我想擁有的內容。我得到的問題是空白單元格,我希望它們被重新裝入,但是它們仍然保留在頂部的任何排序。這是我的排序代碼,任何想法?VBA對空白單元格進行排序

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 

我需要一個解決方案,我可以使用第二個鍵將它們(空白)排序到底部。我不需要一個解決方案,我刪除它們,因爲一些重複項在這一列中只有空白值,如果他們沒有任何替代項,我仍然需要保留它們。

+0

您正在使用一列或基於特定列對整個表進行排序? –

+0

我排序了一個基於這兩列的整個表格,先是列A降序,然後作爲第二個源我把P列在我想要的地方,如果有可能只保留一些文字的Ps,所以當我將Remix拷貝到只有一個在P中的文本將不會被刪除 – Vedad

+0

而空格僅位於一列中,對嗎?你只想把它們移到底部? –

回答

-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 
+0

我不想刪除它們,我希望它們在第二個鍵的底部,因爲我還需要一些空白 – Vedad

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)

+0

我還可以製作更簡單的解決方案,例如添加另一個非空值爲1和0的列,空的,但沒有任何選項,只是通過第二次排序將空白留在底部我的意思是常見的VBA中必須有一些指令並不是什麼大不了的事? – Vedad

+0

我不這麼認爲,它是如何進行排序的,先保留空白(沒有任何值在任何值之前)。你可以降序而不是升序(這會使空白處於底部,但我認爲這不是你所需要的)。您需要一些解決方法,比如我發佈的解決方案。 –

+0

這並不這樣做,但我排序它保持空白升序或降序像排序不工作 – Vedad

相關問題