使用 IsNumeric 删除数据行

Using IsNumeric to delete rows of data

我正在使用 VBA 清理两列数据。如果 A 列中的值是非数字或空白,我需要删除整行。下面是我尝试使用的数据和代码示例。如果 IsNumeric returns false,它似乎完全跳过了删除行的代码部分。

9669    DONE
9670    OPEN
Order # STATUS

9552    
9672    

无效的代码。

Dim cell As Range

For Each cell In Range("A1:A" & max_col)
    If IsNumeric(cell) = False Then
        Cells(cell, 1).Select
        Rows(cell).EntireRow.Delete
        Exit For
    End If
Next cell

感谢任何帮助!

从底部循环

Dim max_col as long
max_col = 100
Dim i as Long
For i = max_col to 1 step -1
   If Not isnumeric(activesheet.cells(i,1)) then
       activesheet.rows(i).delete
   End If
Next i

当删除(或添加,就此而言)行时,您需要通过数据集向后循环 - 请参阅@ScottCraner 的示例以获得类似的确切答案 - 或者,您创建要删除的单元格范围,然后删除一次,如下所示:

Dim rowNo as Long

For rowNo = 1 to max_col

    Dim cell as Range
    Set cell = Range(rowNo,1) 

    If IsNumeric(cell) Then

        Dim collectRows as Range
        If collectRows is Nothing Then
            Set collectRows = cell
        Else
            Set collectRows = Union(collectRows,cell)
        End If

    End If

Next

collectRows.EntireRow.Delete

只使用

    With Range("A1", Cells(Rows.Count, 1).End(xlUp))
        .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        .SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
    End With

或者,如果您不确定数字单元格是否为空

    With Range("A1", Cells(Rows.Count, 1).End(xlUp))
        If WorksheetFunction.CountBlank(.Cells) > 0 Then .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
        If WorksheetFunction.Count(.Cells) < .Rows.Count Then .SpecialCells(xlCellTypeConstants, xlTextValues).EntireRow.Delete
    End With