Macro to delete rows not containing "-" in column A.

imimin

Active Member
Joined
May 9, 2006
Messages
404
Hello!

I have the following macro that will 'examine' column A and delete any row that contains "-".

***What I need for it to do is to delete rows that DO NOT contain the hythen ("-") in column A. Can someone please help me re-write this?

Code:
Sub delrowsB()
    Dim i As Long
        
        For i = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
            'If InStr(Cells(i, "A").Text, "Pc.") Or InStr(Cells(i, "A").Text, "Total") Or InStr(Cells(i, "A").Text, "Delivery") Then
             If InStr(Cells(i, "A").Text, "-") Then
                Cells(i, "A").EntireRow.Delete
            End If
        Next i
            
End Sub

Thank you!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
One quick way (VBA or manullay) is to use a working column to test for the hypen, then use AutoFilter to kill the TRUE rows

This code inserts a formula in the first blank column checking A2 down to the last used cell in column A
=ISERROR(FIND("-",$A2))
As a check for TRUE rows to be deleted

Cheers

Dave

Code:
Sub KillnonDash()
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    Set rng1 = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious)
    Set rng2 = Cells.Find("*", , xlValues, , xlByRows, xlPrevious)
    Set rng3 = Range(Cells(rng2.Row, rng1.Column), Cells(1, rng1.Column))
    Application.ScreenUpdating = False
    Rows(1).Insert
    With rng3.Offset(0, 1)
        .FormulaR1C1 = "=ISERROR(FIND(""-"",RC1))"
        .AutoFilter Field:=1, Criteria1:="TRUE"
        .EntireRow.Delete
        .EntireColumn.Delete
    End With
    Rows(1).Delete
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
The code currently looks to work from row 1 till the last used rows with this line
Code:
  Set rng3 = Range(Cells(rng2.Row, rng1.Column), Cells(1, rng1.Column))

so to work with a header row we need to work from the second cell, and use row 2 for an insert and delete row for the filter

hth

Dave
Code:
Sub KillnonDash()
    Dim rng1 As Range, rng2 As Range, rng3 As Range
    Set rng1 = Cells.Find("*", , xlValues, , xlByColumns, xlPrevious)
    Set rng2 = Cells.Find("*", , xlValues, , xlByRows, xlPrevious)
    Set rng3 = Range(Cells(rng2.Row, rng1.Column), Cells(2, rng1.Column))
    Application.ScreenUpdating = False
    Rows(2).Insert
    With rng3.Offset(0, 1)
        .FormulaR1C1 = "=ISERROR(FIND(""-"",RC1))"
        .AutoFilter Field:=1, Criteria1:="TRUE"
        .EntireRow.Delete
        On Error Resume Next
        'in case all rows have been deleted
        .EntireColumn.Delete
        On Error GoTo 0
    End With
    Rows(2).Delete
    Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,421
Members
452,913
Latest member
JWD210

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top