Macro to delete all rows except .

Sunline

Well-known Member
Joined
Oct 6, 2007
Messages
701
Office Version
  1. 2016
Platform
  1. Windows
Hello all , i was wanting to get a basic macro that can delete all rows using col O except when Mdn is mentioned .
Also leave row 1 as heading , dont delete row 1 .
Thanks .
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Good night!!

See if exemple can help you.

Sub DeletRows()

FinalRow = Range("A65536").End(xlUp).Row
i = 2

Do While i <= FinalRow
If Cells(i, 1) <> "Mdn" Or Cells(i, 1) = "" Then
Cells(i, 1).EntireRow.Delete
FinalRow = FinalRow - 1
Else
i = i + 1
End If
Loop

End Sub
 
Upvote 0
Assuming that Column O will be the column that holds the last row of data: If any other Column, then adjustments will have to be made.


Code:
Sub test()
    Dim LR As Long
    Application.ScreenUpdating = False
    LR = ActiveSheet.Range("O" & Rows.Count).End(xlUp).Row
    For r = LR To 2 Step -1
        If (Range("O" & r).Value <> "Mdn") Then
            Rows(r).Delete
            Application.ScreenUpdating = True
        End If
    Next r
End Sub
 
Upvote 0
This could be as simple as:

Code:
Sub removingrows()
    With [A1].CurrentRegion
        .AutoFilter 15, "<>Mdn"
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
End Sub

Wigi
 
Last edited:
Upvote 0
How about this so you don't have to loop thru the cells...

Code:
Sub DeleteUnwanted()
    Const TextToKeep As String = "Mdn" '<-- Change to suit
    Application.ScreenUpdating = False
    With Range("O1", Range("O" & Rows.Count).End(xlUp))
        .AutoFilter Field:=1, Criteria1:="<>" & TextToKeep
        .Offset(1).EntireRow.Delete
        .AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks for all replies . i failed to mention i have 171,000 rows to get thru so if some speed could be added that would be great .

Jeff i tried yours and it deleted my entire sheet except row1 headings .

wigi yours keeps running but not sure if working , what would be expected time to look thru this many rows .

Nalani same as wigi .

marreco just about to try yours now but am thinking may keep running on . Thanks .
 
Upvote 0
Try this.. supposed your last data is in column O

Code:
Sub delmdn()
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "O").End(xlUp).Row
For i = lr To 2 Step -1
    For x = 1 To 15
        If Cells(i, x).Text = "Mdn" Then
            Exit For
        End If
        If x = 15 And Cells(i, x).Text <> "mdn" Then Cells(i, x).EntireRow.Delete
    Next
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this.. supposed your last data is in column O

Rich (BB code):
Sub delmdn()
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "O").End(xlUp).Row
For i = lr To 2 Step -1
    For x = 1 To 15
        If Cells(i, x).Text = "Mdn" Then
            Exit For
        End If
        If x = 15 And Cells(i, x).Text <> "mdn" Then Cells(i, x).EntireRow.Delete
    Next
Next
Application.ScreenUpdating = True
End Sub

Just correcting the value in red..
Rich (BB code):
Sub delmdn()
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "O").End(xlUp).Row
For i = lr To 2 Step -1
    For x = 1 To 15
        If Cells(i, x).Text = "Mdn" Then
            Exit For
        End If
        If x = 15 And Cells(i, x).Text <> "Mdn" Then Cells(i, x).EntireRow.Delete
    Next
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,351
Members
452,907
Latest member
Roland Deschain

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