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

RWayneHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ShumsExcel & VBA ExpertCommented:
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

Martin LissOlder than dirtCommented:
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

RWayneHAuthor Commented:
Well the Sheet name is really not Sheet2, it chg's need it to be whatever the active sheet is.
Ensure Business Longevity with As-A-Service

Using the as-a-service approach for your business model allows you to grow your revenue stream with new practice areas, without forcing you to part ways with existing clients just because they don’t fit the mold of your new service offerings.

ShumsExcel & VBA ExpertCommented:
Then change

Set Ws = ActiveSheet

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Change
Set Ws = Worksheets("Sheet2")

to

Set Ws = Activesheet
RWayneHAuthor Commented:
Thanks for the help.
ShumsExcel & VBA ExpertCommented:
You're Welcome RwayneH! Glad I was able to help :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.