Link to home
Start Free TrialLog in
Avatar of RWayneH
RWayneHFlag for United States of America

asked on

Rewriting a Delete Rows Sub

How would I adj this code to delete everything that is in column H, that is not an "M"?  However I need to do this on the activate sheet tab.

Sub Delete_Data()
    Set stInp = Sheets("Sheet2")
    oriInputCnt = stInp.Cells(stInp.Rows.Count, "H").End(xlUp).Row
    If oriInputCnt > 5 Then stInp.Rows("6:" & oriInputCnt).EntireRow.Delete Shift:=xlUp
End Sub

Open in new window

Avatar of Shums Faruk
Shums Faruk
Flag of India image

Hi Rwayneh,

Please try below:
Sub Deletes()
Dim Ws As Worksheet
Dim LR As Long, i As Long
Dim v As Variant

Set Ws = Worksheets("Sheet2")
LR = Ws.Range("H" & Rows.Count).End(xlUp).Row

For i = LR To 6 Step -1
    v = Ws.Cells(i, "H").Value
    With Ws.Cells(i, "H")
        If v <> "M" Then
            .EntireRow.Delete
        End If
    End With
Next i
      
End Sub

Open in new window

For no points you don't need the v variable.

Sub Deletes()
Dim Ws As Worksheet
Dim LR As Long, i As Long

Set Ws = Worksheets("Sheet2")
LR = Ws.Range("H" & Rows.Count).End(xlUp).Row

For i = LR To 6 Step -1
    With Ws.Cells(i, "H")
        If .Value <> "M" Then
            .EntireRow.Delete
        End If
    End With
Next i
      
End Sub

Open in new window

Avatar of RWayneH

ASKER

Well the Sheet name is really not Sheet2, it chg's need it to be whatever the active sheet is.
ASKER CERTIFIED SOLUTION
Avatar of Shums Faruk
Shums Faruk
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Change
Set Ws = Worksheets("Sheet2")

to

Set Ws = Activesheet
Avatar of RWayneH

ASKER

Thanks for the help.
You're Welcome RwayneH! Glad I was able to help :)