+ Reply to Thread
Results 1 to 5 of 5

Search and replace macro

Hybrid View

  1. #1
    Registered User
    Join Date
    05-31-2007
    Posts
    3

    Search and replace macro

    Hello,
    I'm writing a macro similar to the search and replace function of excel. What I'm trying to do is:
    a. Search the entire worksheet for a specific word.
    b. If found, replace that specific text (in that particular cell) with different font color.

    Using the search and replace functions of excel, when I change the format of the "replace with", upon Replace, the entire cell format was change. I only need to change the specific text the I've searched.
    Could someone help me get this working. Thanks.

  2. #2
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    This
    Sub change_to_colour(in_range As Range, what As Variant, clr As Long)
        
        With in_range
            Set c = .Find(what, LookIn:=xlValues, lookat:=xlPart)
            If Not c Is Nothing Then
                firstAddress = c.Address
                Do
                    c.Characters(Start:=InStr(1, ucase(c.Value), ucase(what)), Length:=Len(what)).Font.ColorIndex = clr
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstAddress
            End If
        End With
    
    End Sub
    will search through all the cells in "in_range" for the string called "what" and change it to colorindex clr. Try it out with
    Sub test()
    ' change "word" to whatever you're trying to highlight
        change_to_colour ActiveSheet.UsedRange, "word", 3
    End Sub

    Col


    [edit] Changed code to use UCase [/edit]
    Last edited by colofnature; 05-31-2007 at 10:06 AM.
    If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.

  3. #3
    Registered User
    Join Date
    05-31-2007
    Posts
    3
    Thanks for a quick a reply. But what if I want to change the search text with different text? I also noticed that if there are 2 or 3 search words in the same cell, only the first occurrence was change to red fonts. would it be possible to change all of them?

  4. #4
    Forum Contributor colofnature's Avatar
    Join Date
    05-11-2006
    Location
    -
    MS-Off Ver
    -
    Posts
    301
    This will change all instances of the word in any cell
    Sub change_to_colour(in_range As Range, what As Variant, clr As Long)
        
        With in_range
            Set c = .Find(what, LookIn:=xlValues, lookat:=xlPart)
            If Not c Is Nothing Then
                firstAddress = c.Address
                Do
                    c.Characters(Start:=InStr(1, UCase(c.Value), UCase(what)), Length:=Len(what)).Font.ColorIndex = clr
                    foundText = InStr(1, UCase(c.Value), UCase(what))
                    Do While foundText <> 0
                        c.Characters(Start:=InStr(foundText, UCase(c.Value), UCase(what)), Length:=Len(what)).Font.ColorIndex = clr
                        foundText = InStr(foundText + Len(what), UCase(c.Value), UCase(what))
                    Loop
                    Set c = .FindNext(c)
                Loop While Not c Is Nothing And c.Address <> firstAddress
            End If
        End With
    
    End Sub
    To substitute the string you're searching for with a different string use a normal Replace command.


    Col

  5. #5
    Registered User
    Join Date
    05-31-2007
    Posts
    3
    hello,
    I used the replace command but when I replace 2 words in same cell, for example, I first replace "after market" with "aftermarket" then "amongst" with "among", the format of the first word replace was reset and only the second word was change to red fonts. It seems that there's a problem with the Replace command, do you have any idea about this?

+ 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