Search code examples
excelvbaisnumeric

Using IsNumeric to delete rows of data


I have two columns of data I am cleaning up using VBA. If the value in column A is non-numeric or blank, I need to delete the entire row. Below is a sample of the data and the code I am trying to use. It seems to be completely skipping over the portion of the code that deletes the rows if IsNumeric returns false.

9669    DONE
9670    OPEN
Order # STATUS

9552    
9672    

Code that isn't working.

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

Any help is appreciated!


Solution

  • use just

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

    or, if you don't know for sure whether there will be empty or not numeric cells

        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