hip2b2
Board Regular
- Joined
- May 5, 2003
- Messages
- 135
- Office Version
- 2019
- Platform
- Windows
I am using the code below to delete a row where any one of a variety of criteria might be met. I expect those who know will find the code to be something of a dog's breakfast as it is a poidge of things I have found on the net.
The issue is that when the text in Col S is marked "Closed" and Col A in the Row immediatly following is not empty both the row marked Closed and the following row are deleted (whereas only the row marked Closed should be deleted)
Help would be greatly appreciated,
With thanks in advance.
hip
The issue is that when the text in Col S is marked "Closed" and Col A in the Row immediatly following is not empty both the row marked Closed and the following row are deleted (whereas only the row marked Closed should be deleted)
VBA Code:
Dim a As Variant, b As Variant, myVals As Variant, oneVal As Variant
Dim nc As Long, i As Long, k As Long, lr As Long
Const strVals As String = "Closed|closed"
myVals = Split(strVals, "|")
nc = Cells.Find(What:="*", LookIn:=xlFormulas, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
lr = Range("A" & Rows.Count).End(xlUp).Row
a = Application.Index(Cells, Evaluate("Row(7:" & lr & ")"), Array(1, 11))
ReDim b(1 To UBound(a), 1 To 1)
For i = 2 To UBound(a)
If Len(a(i, 1)) > 0 Then
For Each oneVal In myVals
If InStr(1, a(i - 1, 2), oneVal, vbTextCompare) Then
b(i, 1) = 1
k = k + 1
Exit For
End If
Next oneVal
End If
Next i
If k > 0 Then
With Range("A7").Resize(UBound(a), nc)
.Columns(nc).Value = b
.Sort Key1:=.Columns(nc), Order1:=xlAscending, Header:=xlNo
.Resize(k).EntireRow.Delete
End With
End If
'Delete Rows based on criteria
Set h = Sheets("For acting")
If h.AutoFilterMode Then h.AutoFilterMode = False
lr = Columns("A:K").Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
With h.Range("A6:K" & lr)
.AutoFilter Field:=11, Criteria1:=Split("Closed|ABC|ABC Only|XYZ ONLY, Closed|XYZ ONLY; Closed", "|"), Operator:=xlFilterValues
.Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
.AutoFilter Field:=11
Help would be greatly appreciated,
With thanks in advance.
hip