使用IsNumeric删除数据行

时间:2018-03-20 19:28:00

标签: excel vba isnumeric

我使用VBA清理了两列数据。如果A列中的值是非数字或空白,我需要删除整行。下面是我尝试使用的数据和代码示例。如果IsNumeric返回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

感谢任何帮助!

3 个答案:

答案 0 :(得分:4)

仅使用

    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

答案 1 :(得分:3)

从底部循环

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

答案 2 :(得分:3)

当删除(或添加)行时,您需要在数据集中向后循环 - 请参阅@ 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