Solved

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

Posted on 2009-05-04
5
442 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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 Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

820 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