+ Reply to Thread
Results 1 to 6 of 6

Thread: Remove word(s) from string beginning with words from this array

  1. #1
    Registered User
    Join Date
    11-21-2011
    Location
    boston, ma
    MS-Off Ver
    Excel 2007
    Posts
    17

    Talking Remove word(s) from string beginning with words from this array

    I've been working on a major project and I'm almost there. Thanks for anyone on the forum who has helped!

    I would word(s) from a text string which beginning with a specific array of characters.

    ie: The fox jumped over the fence

    Array to remove: fox, jump, turtle, etc

    output for this example: The over the fence

    I'll then do this for 27k rows.

    Thanks!

  2. #2
    Valued Forum Contributor dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    New Delhi, India
    MS-Off Ver
    Excel 1997, 2000, XP, 2003, 2007, 2010
    Posts
    2,398

    Re: Remove word(s) from string beginning with words from this array

    Hi agf,

    Is it always going to be second word in the sentence, else you need to provide the complete list containing fox, turtle etc. Thanks.

    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    DILIPandey
    +919810929744
    dilipandey@gmail.com

  3. #3
    Registered User
    Join Date
    11-21-2011
    Location
    boston, ma
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Remove word(s) from string beginning with words from this array

    Hi Dilipandey,

    The short answer is no, it will not always be the second word in the sentence. I'm otherwise a little confused on your response. Is there something I can edit later to include a complete list? Thank you.

    agf

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Remove word(s) from string beginning with words from this array

    Hello agf,

    This macro starts with cell "A1" on "Sheet1" and continues down column "A" to the last cell with data. Case is ignored and full word matches are made. Words like "jump" and "jumped" are seen as two different words. All occurrences within the string will be replaced.

    Add a new VBA module to your workbook's VBA project. Copy and paste the code below into it. You can then call it from your VBA code or running manually. Change the worksheet name and starting cell to match where your data is located.
    
    ' Thread:  http://www.excelforum.com/excel-general/806819-remove-word-s-from-string-beginning-with-words-from-this-array.html
    ' Poster:  agf12555
    ' Written: December 22, 2011
    ' Author:  Leith Ross
    
    Sub RemoveWords()
    
        Dim Data As Variant
        Dim DeleteWords As Variant
        Dim RegExp As Object
        Dim Rng As Range
        Dim RngEnd As Range
        Dim Text As String
        Dim Wks As Worksheet
        
            Set Wks = Worksheets("Sheet1")
            Set Rng = Wks.Range("A1")
            
            DeleteWords = Array("fox", "jumped", "turtle")
            
              ' Autosize the range
                Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
                Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
                
              ' Add tags to parse full words only.
                For I = 0 To UBound(DeleteWords)
                    DeleteWords(I) = "\b" & DeleteWords(I) & "\b"
                Next I
                
              ' Use pattern matching to replace the words
                Set RegExp = CreateObject("VBScript.RegExp")
                RegExp.Global = True
                RegExp.IgnoreCase = True
                RegExp.Pattern = Join(DeleteWords, "|")
                
                  ' Copy cell values into an array
                    Data = Rng.Value
                    
                      ' Match and replace the words
                        For I = 1 To UBound(Data, 1)
                            Text = Data(I, 1)
                            Text = RegExp.Replace(Text, "")
                            Data(I, 1) = Application.WorksheetFunction.Trim(Text)
                        Next I
                    
                  ' Copy the modified array back into the cells
                    Rng.Value = Data
               
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    11-21-2011
    Location
    boston, ma
    MS-Off Ver
    Excel 2007
    Posts
    17

    Re: Remove word(s) from string beginning with words from this array

    Keith,

    I really appreciate you help. Is there any way to alter it so that it removes words beginning with characters rather than necessarily matching whole words?
    If not, your code will still be useful and I do appreciate it. Thank you!

    agf

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Remove word(s) from string beginning with words from this array

    Hello agf,

    That is an easy fix. Find the For loop shown below in the macro and replace it with code below.
              ' Add tags to parse full words only.
                For I = 0 To UBound(DeleteWords)
                    DeleteWords(I) = "\b" & DeleteWords(I) & "*\b"
                Next I
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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.2.0