Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-04
5
Medium Priority
?
460 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 2000 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

688 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