+ Reply to Thread
Results 1 to 8 of 8

Search and Replace words in bold

Hybrid View

  1. #1
    Registered User
    Join Date
    12-27-2012
    Location
    denmark
    MS-Off Ver
    Excel 2003
    Posts
    4

    Search and Replace words in bold

    Hi there,
    I am looking for a "search and replace routine", that takes all the word that are in bold and puts || around it.
    Bold word ---> |Bold word| - does anyone know how I can do that?

    Your help will truely be appreciated

    Moochie

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Search and Replace words in bold

    Hello moochie,

    Welcome to the Forum!
    Are you looking for a single word or phrase in bold type or are you looking in phrases for words in that are bold type?

    Word
    These words are in bold.

    OR

    Some of these words are in bold.
    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!)

  3. #3
    Registered User
    Join Date
    12-27-2012
    Location
    denmark
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Search and Replace words in bold

    Hi Leith,
    Words in bold.

    Ex. "Helenio Herrera Gavilán" should be changed to "|Helenio| Herrera |Gavilán|"

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

    Re: Search and Replace words in bold

    Hello moochie,

    These words are in worksheet cells, yes?

  5. #5
    Registered User
    Join Date
    12-27-2012
    Location
    denmark
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Search and Replace words in bold

    Yes they are in cells.

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Search and Replace words in bold

    One way -- select the cells of interest and run this:

    Sub Bold()
        Dim cell As Range
        Dim iPos As Long
        Dim sText As String
        
        For Each cell In Selection.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
            sText = " " & cell.Value
            iPos = 0
            
            Do
                iPos = InStrRev(sText, " ", iPos - 1)
                If cell.Characters(iPos, 1).Font.Bold Then
                    cell.Characters(iPos, 0).Insert "|"
                End If
                
                If cell.Characters(iPos - 2, 1).Font.Bold Then
                    cell.Characters(iPos - 1, 0).Insert "|"
                End If
            Loop While iPos > 1
        Next cell
    End Sub
    EDIT: I wouldn't expect it to be fast -- there's a lot of readin' and writin' the worksheet.
    Last edited by shg; 12-27-2012 at 04:11 PM.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Registered User
    Join Date
    12-27-2012
    Location
    denmark
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: Search and Replace words in bold

    Thank you for the help.
    I have one small problem left.

    Som of the my words are half bold and half regular font. ( Like this Community).
    I think the script checks for space, because i does not put the "|" in the these words.

    Words like "Community" end up like "Community|" and they should end up like this "|Com|munity"

    Could you help me with that?
    Last edited by moochie; 12-27-2012 at 09:56 PM.

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Search and Replace words in bold

    That doesn't seem to agree with bold words, but OK:
    Sub Bold()
        Dim cell        As Range
        Dim i           As Long
        Dim bBoldIs     As Boolean
        Dim bBoldWas    As Boolean
    
        For Each cell In Selection.Cells.SpecialCells(xlCellTypeConstants, xlTextValues)
            bBoldWas = False
            For i = Len(cell.Value) To 2 Step -1
                bBoldIs = cell.Characters(i, 1).Font.Bold
                If bBoldIs Xor bBoldWas Then
                    cell.Characters(i + 1, 0).Insert "|"
                    If bBoldIs Then cell.Characters(i + 1, 1).Font.Bold = False
                End If
                bBoldWas = bBoldIs
            Next i
            
            If cell.Characters(1, 1).Font.Bold Then
                cell.Characters(1, 0).Insert "|"
                cell.Characters(1, 1).Font.Bold = False
            End If
        Next cell
    End Sub
    Last edited by shg; 12-28-2012 at 04:58 PM.

+ 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