Solved

Excel VBA - Find a row containing a keyword and other rows in relation to the find.

Posted on 2009-05-04
5
426 Views
Last Modified: 2012-05-06
I need to search every row in a document for a set keyword. On the instances it is found I need the code to return the row # and then select the row before the instance, the row containing the instance, and the two rows after it so they can all be deleted. What is the easiest way of accomplishing this?
0
Comment
Question by:ThePATMAN26
  • 3
  • 2
5 Comments
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 500 total points
ID: 24296333
Assuming you are searching A Column, You can use the following cod which will start from row-1 and will go to the last used row of A Column..
Saurabh...

Sub delete()

    Application.ScreenUpdating = False
 

    Dim i As Long

    i = 1

    Do Until i > Cells(65536, "a").End(xlUp).Row

        If Cells(i, "a").Value = "Your keyword here which you are searching for" Then

            Rows(i - 1 & ":" & i + 2).delete

        Else

            i = i + 1

        End If

    Loop
 

    MsgBox "Done"
 
 

    Application.ScreenUpdating = True
 

End Sub

Open in new window

0
 

Author Comment

by:ThePATMAN26
ID: 24298014
Saurabh,

That works perfectly - but I need to revise my question a little bit. The document I am searching has a varying number of spaces before the instances of the word I am looking for. As I don't know the combination of the number of spaces plus the keyword it doesn't always work. How would I write the code if I needed to account for different characters and spaces at the beginning of the word "apple' (for example.)
0
 
LVL 59

Assisted Solution

by:Saurabh Singh Teotia
Saurabh Singh Teotia earned 500 total points
ID: 24298619
You can change the line-7 and add a trim to it, to trim the additional spaces you have in your data...like this...
If trim(Cells(i, "a").Value) = "Your keyword here which you are searching for" Then
Or alternaitvely you can use this If statement as well..

If instr(trim(Cells(i, "a").Value) , "Your keyword here which you are searching for")>0 Then  
Both the statements will do what you are looking for..
Saurabh...
0
 

Author Comment

by:ThePATMAN26
ID: 24305508
One more question actually - I now want to sub the keyword with a string variable but can't seem to make it happen.
0
 

Author Comment

by:ThePATMAN26
ID: 24305646
nevermind my last comment - figured it out. Thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now