【问题标题】:Excel VBA: Find empty cell and delete the rowExcel VBA:查找空单元格并删除该行
【发布时间】:2015-08-08 08:34:58
【问题描述】:

我的垂直数据在 B:G 列中有间隙

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

  1. B 列的屏幕
  2. 找到一个空单元格
  3. 删除空单元格的整行
  4. 重复这个直到找到 10 个空单元格(这是最棘手的,因为它不应该删除这 10 个空单元格)//10 只是一个没有更多数据的任意数字
  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

【问题讨论】:

    标签: vba excel


    【解决方案1】:
    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 + i 可以更改为 sourceCol = x + i 其中 x 是您的起始列

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      • 2018-09-23
      • 2017-03-10
      • 1970-01-01
      相关资源
      最近更新 更多