+ Reply to Thread
Results 1 to 6 of 6

Replace characters but keep format

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Replace characters but keep format

    The Replace function does not seem to keep formatting when applied to cells with more than one format (different fonts, sizes, superscript/subscript, etc).
    Is there a way to keep the formats intact?
    The example below replaces the characters "and" with "but", but all other formatting is lost.
    Sub Replace_and_keep_Format()
    Dim cell As Range, sentence As String
        For Each cell In Selection
            With cell
                    sentence = Replace$(.Value, "and", "but")
                    .Value = Left$(sentence, Len(sentence) - 0)
            End With
        Next cell
    End Sub

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

    Re: Replace characters but keep format

    Try
    
    Sub test()
        Dim r As Range, x As Long
        For Each r In Selection
            x = InStr(r.Value, "and")
            Do While x > 0
                r.Characters(x, Len("and")).Text = "but"
                x = InStr(x + 1, r.Value, "and")
            Loop
        Next
    End Sub
    Last edited by jindon; 07-31-2017 at 11:06 AM.

  3. #3
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Replace characters but keep format

    Thank you very much @jindon.

  4. #4
    Registered User
    Join Date
    02-21-2022
    Location
    Northampton, England
    MS-Off Ver
    2016
    Posts
    2

    Re: Replace characters but keep format

    Thanks for a great solution.
    This works for me however, if I have a cell that has over 245 approx charaters, the words are not replaced.
    The text format is perfect.

    The code I am using is:

    Sub SearchReplace()
    Dim r As Range, x As Long
    For Each r In Selection
    x = InStr(r.Value, "XXX")
    Do While x > 0
    r.Characters(x, Len("XXX")).Text = learner
    x = InStr(x + 1, r.Value, "XXX")
    Loop
    Next
    End Sub


    I am replaceing "XXX" with a Public declared variable called "learner" which is a String.

    The code does work if the cell has less than 245 characters.

    Can anyone help me solve this problem please.
    Last edited by Robgui; 02-22-2022 at 09:21 PM.

  5. #5
    Forum Contributor
    Join Date
    11-10-2009
    Location
    Perth, Australia
    MS-Off Ver
    Excel 2007
    Posts
    549

    Re: Replace characters but keep format

    Try this to store characters and fonts in array.
    Example replaces "and" with "but" which are same length. Should be able to change for different length strings when taking into account the font array.


    Option Explicit
    
    Sub More_than_256()
        Dim Cell As Range
        
        Dim FontArray As Variant
        Dim TextArray As Variant
        Dim i As Long
        
        Dim the_string As String
    
        For Each Cell In Selection
    
            ReDim FontArray(Len(Cell) - 1)
            For i = 1 To Len(Cell)
                FontArray(i - 1) = Cell.Characters(i, 1).Font.Name
            Next
    
            ReDim TextArray(Len(Cell) - 1)
            For i = 1 To Len(Cell)
                TextArray(i - 1) = Cell.Characters(i, 1).Text
            Next
    
            For i = 0 To Len(Cell) - 1
                Cell.Characters(i + 1, 1).Text = TextArray(i)
            Next i
            
            the_string = Join(TextArray, "")
    
        the_string = Replace(the_string, "and", "but")
        Cell.Value = the_string
    
            For i = 0 To Len(Cell) - 1
                Cell.Characters(i + 1, 1).Font.Name = FontArray(i)
            Next i
    
        Next Cell
    End Sub

  6. #6
    Registered User
    Join Date
    02-21-2022
    Location
    Northampton, England
    MS-Off Ver
    2016
    Posts
    2

    Re: Replace characters but keep format

    Many thanks Un-Do Re-Do.
    I'll give this ago

+ 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] Replace accented characters in worksheet name to regular characters
    By kingsdime29x in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2017, 10:59 AM
  2. [SOLVED] find and replace to not replace characters found as wildcards
    By sabutler4 in forum Excel General
    Replies: 4
    Last Post: 07-03-2013, 06:48 PM
  3. Macro to replace European characters with non "special" English characters?
    By johanna0507 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2012, 09:13 AM
  4. Replace certain characters
    By brownbread in forum Excel General
    Replies: 1
    Last Post: 06-18-2010, 12:00 PM
  5. Replace characters
    By ajaykgarg in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-05-2010, 07:33 PM
  6. fin/replace and keep some characters
    By skullk in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-11-2009, 10:31 AM
  7. How do I replace just a few characters?
    By Tomsriv in forum Excel General
    Replies: 3
    Last Post: 08-14-2006, 07:09 PM

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