Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Rows from Excel based on content

Status
Not open for further replies.

hpl2001

Programmer
Dec 18, 2001
105
CA
Hello:

I'm trying to put together a function that will delete a row if the first cell in the row is blank or contains non-numeric characters. I can't just test for the cell to be numeric, because all the cells are formatted as general. A variable number of the rows will have values like '100012122' in cell A - those are the rows I want to keep. I need to delete all the other rows.

Any thoughts?

TIA...Holly
 


hi,

What code do you have?

What code specifically do you have trouble with?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
You might also check out the IsNumeric() function.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I was using this:

Sub DeleteNonNumeric_ColE()
On Error Resume Next
Range("e:e").SpecialCells(xlBlanks, _
xlTextValues).EntireRow.Delete
Range("e:e").SpecialCells(xlConstants, _
xlTextValues).EntireRow.Delete
Range("e:e").SpecialCells(xlFormulas, _
xlTextValues + xlErrors + xlLogical).EntireRow.Delete
On Error GoTo 0
End Sub


...but it deleted everything
 
This code sorta works:

For i = 1 To 8000
If IsNumeric(Cells(i, 1).Value) = False Then
Rows(i).Select
Selection.Delete Shift:=xlUp
End If
Exit For
Next

But it's only looking at the first row, and not deleting it if it's blank
 


give this a try. Just supply a range that correspondes to the column you want data deleted in.
Code:
Sub DeleteNonNumeric(rng As Range)
    Dim lRow As Long
    
    With rng.Parent
        For lRow = .UsedRange.Rows.Count + .UsedRange.Row - 1 To .UsedRange.Row Step -1
            With .Cells(lRow, rng.Column)
                If .Value = "" Then
                    .EntireRow.Delete xlUp
                ElseIf Not IsNumeric(.Cells(1, 1).Value) Then
                    .EntireRow.Delete xlUp
                End If
            End With
        Next
    End With
End Sub


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Got it. Thanks very much for your help.

H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top