简体   繁体   English

Excel VBA:查找空单元格并删除行

[英]Excel VBA: Find empty cell and delete the row

I have vertical data with gaps in columns B:G 我在B:G列中有空白的垂直数据

I wish that my code does the following: 我希望我的代码执行以下操作:

  1. Screens through column B 通过B栏的屏幕
  2. Find an empty cell 查找一个空单元格
  3. Deletes the entire row of the empty cell 删除空单元格的整行
  4. Repeat this until 10 empty cells are found (that's the trickiest, as it should not delete these 10 empty cells) //10 is just an arbitrary number that there is no more data 重复此操作,直到找到10个空单元格(这是最棘手的,因为它不应该删除这10个空单元格)// 10只是一个任意数字,没有更多数据
  5. Then go to column C repeat the full proces and so on until all columns are screened 然后转到C列,重复整个过程,依此类推,直到所有列都被筛选为止

I have some basic VBA knowledge and this is the code I have found on the subject so far, however it's a mess in my head how to approach this. 我有一些基本的VBA知识,这是到目前为止我在该主题上找到的代码,但是如何实现这一点在我头上一团糟。

The main issue I am having is how would the code know when to stop deleting and move to the next column. 我遇到的主要问题是代码如何知道何时停止删除并移至下一列。

This code below finds the next empty cell in column B and selects it. 下面的代码在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

Try above. 尝试以上。 sourceCol = 1 + i can be changed to sourceCol = x + i where x is your start column sourceCol = 1 +我可以更改为sourceCol = x + i其中x是您的起始列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM