Macro deletes all data rather than just intended rows

hip2b2

Board Regular
Joined
May 5, 2003
Messages
135
Office Version
  1. 2019
Platform
  1. Windows
The following VBA code is intended to delete the Rows in Sheet2 where Field 12 contains the string ABC (or ABC and anything else), this works as intended. What I have belatedly discovered is that if Field 12 does not contain any instance of ABC all the data in Sheet2 below Row 6 is deleted.

Where have I messed up?

Thanks in advance.

hip

VBA Code:
Set h = Sheets("For Sheet2")
    If h.AutoFilterMode Then h.AutoFilterMode = False
    lr = Columns("A:L").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With h.Range("A6:L" & lr)
    
    .AutoFilter Field:=12, Criteria1:="=ABC" & strName & "*", Operator:=xlAnd '<Delete Row If Contains: ABC
    .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
    .AutoFilter Field:=12
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
No nothing I see references Col W. Delete criteria is in Field 12 (Col L)
 
Upvote 0
First let me apologize. I have asked many question in the past but have never messed up the asking as badly as I have this time.

Data is copied from Sheet1 to Sheet2. Filter criteria in Sheet2 is contained in Col L. Where the filter term is met the row is be deleted. So far so good!!!

Problem:
If the filter term does not exist (in the case below "Closed", the all the data on sheet 2 (below row 6) are deleted. That is what I am trying to correct.


Set h = Sheets("For Sheet2")
If h.AutoFilterMode Then h.AutoFilterMode = False
lr = Columns("A:L").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With h.Range("A6:L" & lr)

.AutoFilter Field:=12, Criteria1:="Closed" & strName & "*", Operator:=xlAnd '<Delete Row If Contains: Closed
.Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
.AutoFilter Field:=12
 
Upvote 0
Can you upload the workbook to a hosting site...dropbox for instance..then post the link back here please ?
 
Upvote 0
This should do it:


The macro will delete the rows where "Open" or "Closed" are in Field 12 (Col L). If either of these are missing the entire database is deleted.

Again, thanks for sticking with my problem.
 
Upvote 0
Try it this way
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "B").End(xlUp).Row
 For r = lr To 7 Step -1
    If Cells(r, 12) = "Closed" Or Cells(r, 12) = "Open" Then
      Rows(r).Delete
    End If
 Next r
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Try it this way
VBA Code:
Sub MM1()
Dim lr As Long, r As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "B").End(xlUp).Row
 For r = lr To 7 Step -1
    If Cells(r, 12) = "Closed" Or Cells(r, 12) = "Open" Then
      Rows(r).Delete
    End If
 Next r
Application.ScreenUpdating = True
End Sub

Funny how the filter mode almost works; odd that there isn't a "switch" of one sort or another to only delete where the text exists as opposed to doing what it does.

Thanks, for the new macro it looks like it will do what I need to do, I'll test it tomorrow. BTW, would this work if the cells contain the specied text along with other text For example "Open in the morning", or is it restricted to the exact text string entered in the macro?

Regards

hip
 
Upvote 0
The current code is restricted to "Open" or "Closed". If you wanted to expand it use
Rich (BB code):
Sub MM1()
Dim lr As Long, r As Long
Application.ScreenUpdating = False
lr = Cells(Rows.Count, "B").End(xlUp).Row
 For r = lr To 7 Step -1
  If InStr(Cells(r, 12), "Closed") Or InStr(Cells(r, 12), "Open") Then
      Rows(r).Delete
    End If
 Next r
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,626
Messages
6,120,602
Members
448,974
Latest member
ChristineC

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