Consulting

Results 1 to 8 of 8

Thread: Find word and bold

  1. #1
    VBAX Regular
    Joined
    Nov 2004
    Posts
    27
    Location

    Find word and bold

    Hello,

    I would like help getting started on an Excel macro to find all the instances of a user specified word in a column also specified by the user and change the font formatting of the word to bold.

    Regards
    Ron

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Hey Ron,


    Maybe you can use something like this ...

    Option Explicit
    
    Sub MakeAllBold()
        Dim searchRng As Range, strSearch As String, cel As Range
        '** Change the range to suit
        Set searchRng = Range("O5:O19")
        strSearch = InputBox("What would you like to search for?", "Make Bold")
        'If cancel was pressed or nothing was entered, quit running
        If strSearch = "" Then Exit Sub
        '** Loop through each cell in specified range
        For Each cel In searchRng
            '** Check the value against our new variable (user-entered value)
            If cel.Value = strSearch Then
                '** If matching, make it bold
                cel.Font.Bold = True
                '** If not, make it not bold, used mostly for running multiple times
            Else: cel.Font.Bold = False
            End If
        Next cel
    End Sub
    Just put it into a standard Module and run from there. HTH

  3. #3
    VBAX Regular
    Joined
    Nov 2004
    Posts
    27
    Location
    firefytr

    Thanks for the reply. I should have been more specific. Your code works great if one value is in each cell, but I?m working with cells that have one or more sentences, and I would like to bold just the one word the user enters in the input box.

    Ron

  4. #4
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Care to give some examples?

  5. #5
    VBAX Regular
    Joined
    Nov 2004
    Posts
    27
    Location
    Below is an example of the text entered in a cell and I want to bold the word shall.

    The contractor shall perform the specific tasks described in paragraph 4.0 and summarized in the table below. (Reference Table 2-2 page 1)

  6. #6
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Okay then, make use of the Characters mehtod ..

    Sub MakeWordBold()
        Dim strSearch As String, searchRng As Range, i As Long, cel As Range
        Set searchRng = Range("A1:A10")
        strSearch = InputBox("Please enter the text to make bold:", "Bold Text")
        If strSearch = "" Then Exit Sub
        For Each cel In searchRng
            With cel
                .Font.Bold = False
                For i = 1 To Len(.Text) - Len(strSearch) Step 1
                    If Mid(.Text, i, Len(strSearch)) = strSearch Then
                        .Characters(i, Len(strSearch)).Font.Bold = True
                    End If
                Next i
            End With
        Next cel
    End Sub

  7. #7
    VBAX Regular
    Joined
    Nov 2004
    Posts
    27
    Location
    firefytr
    
    Works fine. Thanks for the help!
    
    Best regards
    
    Ron 

  8. #8
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    No problem. Glad to help.

    Btw, did you know you can mark your own threads as Solved? Go to Thread Tools --> Mark Solved --> Perform Action. Take care!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •