Deleting Rows with text

XLerator

New Member
Joined
Apr 10, 2002
Messages
21
I have various data in a column, and I want to include deleting them in a formula macro.

For example, I want the macro to run so that whenever it sees ANY ROW which contains the Text XX, YY, or ZZ etc it erases it.


So the macro would basically look for any of those three values and delete the entire row they are in. It would keep deleting ALL the rows until there are no more occurrences of the text.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Try something like:

<pre/>
Option Explicit
Option Base 1

Sub Delete_Txt()
Dim x As Single
Dim Kount As Double
Dim sSearch()

'// Define your string to search for here
sSearch() = Array("XX", "YY", "ZZ")
Kount = 0
Application.ScreenUpdating = False

For x = 1 To UBound(sSearch())
On Error Resume Next
Do
Cells.Find(What:=sSearch(x), After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False).Activate
Selection.EntireRow.Delete
If Err Then Exit Do
Kount = Kount + 1
Loop
On Error GoTo 0
Next
Application.ScreenUpdating = True

MsgBox "Completed deleting " & Kount & " rows"

End Sub
</pre>
 
Upvote 0
Hi,

Brute force method here. Try,

---begin VBA---
Sub Delete_Rows()
Dim lastrow As Long, lastcol As Integer
Dim x As Long, y As Integer, Counter As Long

Application.ScreenUpdating = False
With ActiveSheet
.UsedRange
lastrow = .Cells.SpecialCells(xlCellTypeLastCell).Row
lastcol = .Cells.SpecialCells(xlCellTypeLastCell).Column

For x = lastrow To 1 Step -1
Counter = 0
Counter = Counter + _
WorksheetFunction.CountIf(Range(Cells(x, 1), Cells(x, lastcol)), "XX") + _
WorksheetFunction.CountIf(Range(Cells(x, 1), Cells(x, lastcol)), "YY") + _
WorksheetFunction.CountIf(Range(Cells(x, 1), Cells(x, lastcol)), "ZZ")
If Counter <> 0 Then Rows(x).delete
Next x

End With

End Sub
---end VBA---

HTH,
Jay
 
Upvote 0

Forum statistics

Threads
1,213,539
Messages
6,114,221
Members
448,554
Latest member
Gleisner2

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