Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

excel macro

Status
Not open for further replies.

Tonyvee73

Technical User
Jul 4, 2018
10
GB
Hi
I am running an excel macro to search for text then delete if found as below. However its also deleting lines with the text "11 order(s)" - is there a way I can specific set to avoid this?


Sub A_Mainsort()

Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Rows(1).EntireRow.Delete
Columns("D:I").Delete
Columns("A:C").HorizontalAlignment = xlCenter
Columns("A:C").AutoFit
Dim c As Range
Dim SrchRng
Set SrchRng = ActiveSheet.Range("B1", ActiveSheet.Range("B65536").End(xlUp))
Do
Set c = SrchRng.Find[highlight #FCE94F]("1 order(s)",[/highlight] LookIn:=xlValues)
If Not c Is Nothing Then c.EntireRow.Delete
Loop While Not c Is Nothing

End Sub
 
You may use optional argument [tt]LookAt:=xlWhole[/tt] in [tt]Find[/tt] method.
Some comments:
1) you can simplify first eight actions with single [tt]Rows("1:8").Delete[/tt]
2) you can use wildcards in [tt]Find[/tt], esp. when you need exact match, for instance search for "11 order(s)*" if the text has to start with "11 order(s)",
3) if you find nothing, VBA throws error, so except of testing [tt]If Not c Is Nothing[/tt] you need some error handling.


combo
 
ok thanks. its just the text 1 order(s) I need it to look for, not different values.
So how would I change my macro to just look for that and not anything else?
 
For exact "1 order(s)" match simply put this as text to search, but with additional LookAt:=xlWhole argument set.

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top