+ Reply to Thread
Results 1 to 9 of 9

Find specific text and substitute characters

Hybrid View

  1. #1
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Find specific text and substitute characters

    I've attached a sample spreadsheet. In Column C the alpha prefixes will be followed by policy numbers. The alpha prefixes can be several different policy types (LX, OX, LCLP, GUAR, etc.) I want to find only those cells that begin with either "LX" or "OX" and substitute "LXOR" for LX and "OXOR" for OX followed by the policy number. NOTE: Column D has been added to show what the prefix/policy number will look like after substitution.

    For example: LX123456789 will now become LXOR123456789

    The number of rows in Column C will vary from spreadsheet to spreadsheet.
    Attached Files Attached Files

  2. #2
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Find specific text and substitute characters

    A formula like = SUBTITUTE(C1, "LX", "LXOR") would work.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  3. #3
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    25,417

    Re: Find specific text and substitute characters

    You can use this formula in D7:

    =IF(OR(LEFT(C7,2)="LX",LEFT(C7,2)="OX"),SUBSTITUTE(SUBSTITUTE(C7,"LX","LXOR"),"OX","OXOR"),C7)

    then copy down.

    Hope this helps.

    Pete

  4. #4
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Find specific text and substitute characters

    That would work, however, I'm wanting to include this in a macro I've already written. The macro includes adding up multiple rows into one total sum for that policy, some sorting and formatting of data, etc.) I would like to include the substitition so that I can do everything without the user having to touch the spreadsheet. Is there a way to incorporate the commands below into vba code to use within my macro?

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Find specific text and substitute characters

    VBA
    Once prefix has been changed, no more change will be made.
    Sub test()
        Dim a, i As Long
        With [a6].CurrentRegion.Columns("c")
            a = .Value
            With CreateObject("VBScript.RegExp")
                .IgnoreCase = True
                .Pattern = "^((L|O)x)(?!OR)"
                For i = 3 To UBound(a, 1)
                    a(i, 1) = .Replace(a(i, 1), "$1OR")
                Next
            End With
            .Value = a
        End With
    End Sub

  6. #6
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Find specific text and substitute characters

    Thank you very much - I tested this and it replaced each line in Column C except the 1st row even though it met the criteria. I couldn't tell by your code what would have to change so it doesn't skip the first row in the column?

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Find specific text and substitute characters

    The code is written based on the data layout in your uploaded file.
    Try change to
                For i =2 To UBound(a, 1)
    Otherwise, I need to see actual data layout.

  8. #8
    Registered User
    Join Date
    10-08-2013
    Location
    Eugene, OR
    MS-Off Ver
    Excel 2010
    Posts
    43

    Re: Find specific text and substitute characters

    That was about the only thing I could think of to change, and that worked perfectly. Thank you so much for taking the time to respond to my inquiry.

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Find specific text and substitute characters

    You are welcome and thanks for the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Need help completing a formula to find and substitute a character in a text string...
    By danielneedssomehelp in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 08-27-2014, 11:32 AM
  2. [SOLVED] Need a formula to find and substitute a word in a text if a condition is met...
    By danielneedssomehelp in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-18-2014, 01:34 PM
  3. Want to Remove A Non-Specific Text Strand Between Two Specific Characters
    By predictablepanda in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-04-2013, 12:33 AM
  4. Replies: 1
    Last Post: 07-19-2012, 02:27 PM
  5. How to use a substitute formula to substitute text entries to a different text entry
    By andybocchi in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-01-2010, 07:50 PM
  6. find and replace specific characters
    By wkilc in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-21-2010, 02:59 PM
  7. find cells that contain specific characters
    By mshornet in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 11-23-2005, 10:10 AM

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