Sub DeleteRowWithContents()
'========================================================================
' DELETES ALL ROWS FROM A2 DOWNWARDS WITH THE WORDs "Record Only" IN COLUMN D
'========================================================================
Last = Cells(Rows.Count, "G").End(xlUp).Row
For i = Last To 1 Step -1
If (Cells(i, "G").Value) = "Balanced" Then
'Cells(i, "A").EntireRow.ClearContents ' USE THIS TO CLEAR CONTENTS BUT NOT DELETE ROW
Cells(i, "A").EntireRow.Delete
End If
Next i
End Sub
Hey Ron,Hi I have used both sets of code within the thread. This successfully deletes or autofilters and deletes one set of criteria. I have the following items that I need to deleted the whole row for.
PAI, MPD, Private Contractor, Local Council. I can succesfully delete one item at a time.
Is there a way to specify in either code multiple criteria I have tried ands and ors with no success. I am new to this guys so any help would be appreciated. The column I need to autofilter or search is "R" and delete all rows that contain any of the four item MPD, PAI, Private Contractor or Local Council
cheers and thanks
Ron
Sub test()
Dim x, lr As Long, lc As Integer
Dim a, b() As Variant, i As Long, e
x = Array("MPD", "PAI", "Private Contractor", "Local Council")
lr = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
lc = ActiveSheet.Cells.Find("*", searchorder:=xlByColumns, _
searchdirection:=xlPrevious).Column
If lc < 18 Then
MsgBox "Column R is unused" & Chr(10) & "Exiting"
Exit Sub
End If
a = Cells(1, "r").Resize(lr)
ReDim b(1 To lr, 1 To 1)
For i = 1 To lr: For Each e In x
If InStr(a(i, 1), e) Then b(i, 1) = 1
Next e, i
Cells(1, lc + 1).Resize(lr) = b
Cells(1, 1).Resize(lr, lc + 1).Sort Cells(1, lc + 1), 1
Cells(1, 1).Resize(Cells(1, lc + 1).End(xlDown).Row, lc + 1).Delete 3
End Sub
aeddipa,I used the VBA code supplied above from the 2nd person who responded. This works great; however, I have over 50,000 rows of data and it is taking an extremely long time to process through this macro. Is there any way to speed up the overall process of this code?
I think this would only happen if none of your specified strings occur in Column R.Tried the code but it deleted everything in the sheet
Sub test2()
Dim x, lr As Long, lc As Integer
Dim a, b() As Variant, i As Long, e, k As Boolean
Application.ScreenUpdating = False
x = Array("MPD", "PAI", "Private Contractor", "Local Council")
lr = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
lc = ActiveSheet.Cells.Find("*", searchorder:=xlByColumns, _
searchdirection:=xlPrevious).Column
If lc < 18 Then
MsgBox "Column R is unused" & Chr(10) & "Exiting"
Exit Sub
End If
a = Cells(1, "r").Resize(lr)
ReDim b(1 To lr, 1 To 1)
For i = 1 To lr: For Each e In x
If InStr(a(i, 1), e) > 0 Then
b(i, 1) = 1
k = True
End If
Next e, i
If k = False Then Exit Sub
Cells(1, lc + 1).Resize(lr) = b
Cells(1, 1).Resize(lr, lc + 1).Sort Cells(1, lc + 1), 1
Cells(1, 1).Resize(Cells(1, lc + 1).End(xlDown).Row, lc + 1).Delete 3
Application.ScreenUpdating = True
End Sub