2015-05-26 244 views
2

我有在B列的差距垂直數據:摹Excel的VBA:查找空單元格,並刪除該行

我希望我的代碼執行以下操作:

  1. 屏幕通過B柱
  2. 查找空單元格
  3. 刪除空單元格的整行
  4. 重複此操作,直至找到10個空單元格(這是最棘手的,因爲它不應該刪除這10個空單元格)// 10只是一個任意的y個,有沒有更多的數據
  5. 然後去C列重複充分理線,以此類推,直到所有的列進行篩選

我有一些基本的VBA知識,這是我所找到的代碼但是到目前爲止,儘管如此,我的腦海裏一團糟。

我遇到的主要問題是代碼如何知道何時停止刪除並移動到下一列。

下面的代碼找到列B中的下一個空單元格並將其選中。

Public Sub SelectFirstBlankCell() 
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer 
Dim currentRowValue As String 

sourceCol = 6 'column F has a value of 6 
rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 

'for every row, find the first blank cell and select it 
For currentRow = 1 To rowCount 
    currentRowValue = Cells(currentRow, sourceCol).Value 
    If IsEmpty(currentRowValue) Or currentRowValue = "" Then 
     Cells(currentRow, sourceCol).Select 
     Exit For 'This is missing... 
    End If 
Next 
End Sub 

回答

0
Public Sub SelectFirstBlankCell() 
Dim sourceCol As Integer, rowCount As Integer, currentRow As Integer 
Dim currentRowValue As String 

For i = 0 To Cells(1, Columns.Count).End(xlToLeft).Column 'count and loop cols 

    sourceCol = 1 + i 'increment with i, move 1 to start column 
    rowCount = Cells(Rows.Count, sourceCol).End(xlUp).Row 

    For currentRow = rowCount To 1 Step -1 ' this will start at end and work its way up 

     currentRowValue = Cells(currentRow, sourceCol).Value 

     If IsEmpty(currentRowValue) Or currentRowValue = "" Or Trim(currentRowValue) = "" Then 'Trim accounts for " " may not be needed 
      Cells(currentRow, sourceCol).EntireRow.Delete 
     End If 

    Next 
Next 
End Sub 

嘗試以上。 sourceCol = 1 +我可以更改爲sourceCol = x + i其中x是您的起始列

相關問題