+ Reply to Thread
Results 1 to 6 of 6

Script or formula to move text from beginning to end of a string

Hybrid View

  1. #1
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Question Script or formula to move text from beginning to end of a string

    Hi,

    In Word I'd use wildcards to do this. I'm not sure how to do it in Excel.

    I need to search a column for the text "Translation of" if that text is found, I need to delete the text and put "with translation" at the end of the string. An added bonus would be if I could have it automatically highlight the cell in Red if it was changed. Here's a sample with before and after tables: Sample.xlsx

    Thanks!
    Rob

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,632

    Re: Script or formula to move text from beginning to end of a string

    Maybe this:

    Formula: copy to clipboard
    =IF(LEFT(B2, 14)="Translation of",TRIM(SUBSTITUTE(B2,"Translation of","")&" with translation"),B2)

  3. #3
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: Script or formula to move text from beginning to end of a string

    Copy code into a standard module (if you don't know how, ask).
    Run: TranslationOf2WithTranslation

    Sub TranslationOf2WithTranslation()
        Dim nRow As Long, nLastRow As Long, s As String
        Dim nLen As Long
        nLen = Len("Translation of ")
        nLastRow = Cells(Rows.Count, "B").End(xlUp).Row
        For nRow = 2 To nLastRow
            s = Cells(nRow, "B")
            If Len(s) >= nLen Then
                If Left$(s, nLen) = "Translation of " Then
                    s = Right$(s, Len(s) - nLen)
                    Cells(nRow, "B") = s & " with translation"
                    Cells(nRow, "B").Interior.Color = vbRed
                End If
            End If
        Next nRow
    End Sub

  4. #4
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Script or formula to move text from beginning to end of a string

    Awesome. Worked like a dream! Thanks!

  5. #5
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: Script or formula to move text from beginning to end of a string

    You can use wildcards in excel also.
    or this macro
    Sub ChangeIt()
      Dim OneCell As Range
      For Each OneCell In Range(Range("B1"), Range("B" & Rows.Count).End(xlUp))
         If InStr(1, OneCell, "Translation of ") >= 1 Then
            OneCell.Value = Application.WorksheetFunction.Substitute(OneCell.Value, "Translation of ", "") & " with translation"
            OneCell.Interior.ColorIndex = 38
         End If
      Next OneCell
    End Sub
    regards

    johnjohns

    When you are not sure where to go, every road takes you there!

  6. #6
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Script or formula to move text from beginning to end of a string

    Quote Originally Posted by johnjohns View Post
    You can use wildcards in excel also.
    Search and Replace doesn't have a "use wildcards" box that will allow you to do things like this
    Find what: Translation of (*)
    Replace with: \1 with translation

    Am I missing something?
    Thanks for all of the help!!!

+ 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