Excel VBA:find空单元格并删除行

我有垂直的数据,在列B:G中有空白

我希望我的代码执行以下操作:

  1. 通过B列屏幕
  2. find一个空单元格
  3. 删除空单元格的整个行
  4. 重复这个操作,直到find10个空单元(这是最棘手的,因为它不应该删除这10个空单元)// 10只是一个任意数字,没有更多的数据
  5. 然后转到C列重复完整的过程,依此类推直到所有列都被筛选出来

我有一些基本的VBA知识,这是迄今为止我在这个主题上find的代码,但是如何解决这个问题在我脑海中是一团糟。

我遇到的主要问题是代码如何知道何时停止删除并移动到下一列。

下面的代码find列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 

 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是您的起始列