+ Reply to Thread
Results 1 to 5 of 5

How to Find and Replace a string if cell does not contain certain texts.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-01-2012
    Location
    new york
    MS-Off Ver
    Excel 2007
    Posts
    14

    How to Find and Replace a string if cell does not contain certain texts.

    Hi I want to create a macro that searches a column for a specific criteria, it will then check the portion being replaced if it contains either "Apt", "Suite", or "Floor"
    before replacing it.

    For Ex if I were to Find "*West" and replace with "", I want it to be able to check to see if "Suite" is in the portion being replaced before replacing it, and if it is then dont replace:

    A1) 123 Fake Street West Apt 1
    A2) 234 Random Street Suite 3 West
    A3) 333 Excel Ave West Floor 3


    Result should be the following.
    b1) Apt 1
    b2) 234 Random Street Suite 3 West
    b3) Floor 3

    any ideas?

    thanks!
    Last edited by windcloud2; 04-04-2012 at 11:47 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-23-2012
    Location
    United States
    MS-Off Ver
    Excel 2010
    Posts
    1,093

    Re: How to Find and Replace a string if cell does not contain certain texts.

    Hello windcloud2,

    This can be done with a formula in a column next to the original cell.

    =RIGHT(A1,LEN(A1)-SEARCH("West",A1)-4)

    Thanks,

    Rvasquez

  3. #3
    Valued Forum Contributor
    Join Date
    03-14-2012
    Location
    Arizona USA
    MS-Off Ver
    Excel 2000/2007
    Posts
    408

    Re: How to Find and Replace a string if cell does not contain certain texts.

    This works.


    Sub Macro1()
    
        FindString = InputBox("Enter the string to find.")
        ReplaceString = InputBox("Enter the replacement string.")
        StartRow = 1
        EndRow = 3
        
        For MyRow = StartRow To EndRow
        
            Range("A" + CStr(MyRow)).Select
        
            Test1 = InStr(Range("A" + CStr(MyRow)).Text, " Apt")
            Test2 = InStr(Range("A" + CStr(MyRow)).Text, " Suite")
            Test3 = InStr(Range("A" + CStr(MyRow)).Text, " Floor")
            If Test1 = 0 And Test2 = 0 And Test3 = 0 Then
                  ActiveCell.Replace What:=FindString, Replacement:=ReplaceString, LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
            End If
        
        Next MyRow
        
    End Sub

  4. #4
    Registered User
    Join Date
    04-01-2012
    Location
    new york
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: How to Find and Replace a string if cell does not contain certain texts.

    Dennis,

    hmm unfortunately that macro still replaces all content.
    When I enter "*West" into the prompt it still deletes everything before "west" even though floor or apt is before it.


    A1) 123 Fake Street Floor 1 Apt 2 West

    If I enter "*West" into the prompt I should get the following because the test failed.

    B1) 123 Fake Street Floor 1 Apt 2 West

  5. #5
    Registered User
    Join Date
    04-01-2012
    Location
    new york
    MS-Off Ver
    Excel 2007
    Posts
    14

    Re: How to Find and Replace a string if cell does not contain certain texts.

    Dennis, I just realized the text is cap sensitive, it works now .. thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1