+ Reply to Thread
Results 1 to 7 of 7

Macro to remove certain text string ONLY if comes at first

Hybrid View

ghoneim Macro to remove certain text... 07-12-2013, 04:48 PM
gjcase Re: Macro to remove certain... 07-12-2013, 05:00 PM
ghoneim Re: Macro to remove certain... 07-12-2013, 05:22 PM
ghoneim Re: Macro to remove certain... 07-13-2013, 10:38 AM
gjcase Re: Macro to remove certain... 07-12-2013, 05:18 PM
gjcase Re: Macro to remove certain... 07-16-2013, 11:02 AM
gjcase Re: Macro to remove certain... 07-16-2013, 11:09 AM
  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Macro to remove certain text string ONLY if comes at first

    Hi,

    I need a macro that helps me check a column of text against a list of given words, remove these words from column cells ONLY when they come at the very first of the cell

    Example:
    I need to remove the word "learn" from a list of course titles in a column (A2: A100) - only when "learn" show up in the beginning:

    learn how to design robots ---------->how to design robots

    While if "learn" show up in any other part (middle, or last word):

    Explore and learn how to design robots --------> stays the same
    Robots designs: Where to learn----------> stays the same


    Any help with that?
    Thanks in Advance

  2. #2
    Forum Contributor
    Join Date
    08-08-2005
    Location
    Kansas, USA
    MS-Off Ver
    2016
    Posts
    293

    Re: Macro to remove certain text string ONLY if comes at first

    How about this:
    Sub Test()
    Dim str As String
    Dim C As Range
    
    For Each C In Selection
        str = C.Value
        If UCase(Left(str, 6)) = "LEARN " Then C.Value = Right(str, Len(str) - 6)
    Next C
    End Sub
    Requires you select the range to be operated on first. If you wanted, you could hard-wire that.

  3. #3
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Macro to remove certain text string ONLY if comes at first

    It works exactly the way I need!

    Thanks a million gjcase
    Last edited by jeffreybrown; 07-13-2013 at 10:40 AM. Reason: As per Forum Rule #12, please don't quote whole post unless necessary -- it's just clutter.

  4. #4
    Registered User
    Join Date
    10-19-2012
    Location
    Egypt
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Macro to remove certain text string ONLY if comes at first

    One more thing gjcase..
    I wonder how would this code look like if -instead of removing "learn ", adding "ing" to it to become "learning"? Still when comes at first of sentence only
    I'd appreciate your response..

    Thanks a lot

  5. #5
    Forum Contributor
    Join Date
    08-08-2005
    Location
    Kansas, USA
    MS-Off Ver
    2016
    Posts
    293

    Re: Macro to remove certain text string ONLY if comes at first

    The above doesn't use a list; the one below does:
    Sub Test()
    Dim str As String
    Dim C As Range
    Dim wrdlist() As Variant
    Dim i As Integer
    Dim l As Integer
    
    
    wrdlist = Range("D2:D6")
    
    
    For Each C In Selection
        str = C.Value
        For i = 1 To UBound(wrdlist)
            l = Len(wrdlist(i, 1)) + 1
            If UCase(Left(str, l)) = UCase(wrdlist(i, 1)) & " " Then C.Value = Right(str, Len(str) - l)
        Next i
    Next C
    
    End Sub
    You need to specify your list of words to be replaced in D2:D6, or revise the macro to point to the correct address.

  6. #6
    Forum Contributor
    Join Date
    08-08-2005
    Location
    Kansas, USA
    MS-Off Ver
    2016
    Posts
    293

    Re: Macro to remove certain text string ONLY if comes at first

    Here you are:
    Sub Test()
    Dim str As String
    Dim C As Range
    Dim wrdlist() As Variant
    Dim i As Integer
    Dim l As Integer
    
    
    wrdlist = Range("D2:D6")
    
    
    For Each C In Selection
        str = C.Value
        For i = 1 To UBound(wrdlist)
            l = Len(wrdlist(i, 1)) + 1
            If UCase(Left(str, l)) = UCase(wrdlist(i, 1)) & " " Then C.Value = Left(str,l) & "ing " & Right(str, Len(str) - l)
        Next i
    Next C
    
    End Sub

  7. #7
    Forum Contributor
    Join Date
    08-08-2005
    Location
    Kansas, USA
    MS-Off Ver
    2016
    Posts
    293

    Re: Macro to remove certain text string ONLY if comes at first

    Here you are:
    Sub Test()
    Dim str As String
    Dim C As Range
    Dim wrdlist() As Variant
    Dim i As Integer
    Dim l As Integer
    
    
    wrdlist = Range("D2:D6")
    
    
    For Each C In Selection
        str = C.Value
        For i = 1 To UBound(wrdlist)
            l = Len(wrdlist(i, 1)) + 1
            If UCase(Left(str, l)) = UCase(wrdlist(i, 1)) & " " Then C.Value = Left(str,l) & "ing " & Right(str, Len(str) - l)
        Next i
    Next C
    
    End Sub

+ 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