+ Reply to Thread
Results 1 to 6 of 6

How to find a string and change it's color?

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    How to find a string and change it's color?

    Can anyone tell me which command would find a certain word in a range, then change it's color please?

  2. #2
    Forum Expert sweep's Avatar
    Join Date
    04-03-2007
    Location
    Great Sankey, Warrington, UK
    MS-Off Ver
    2003 / 2007 / 2010 / 2016 / 365
    Posts
    3,446

    Re: How to find a string and change it's color?

    Hi,

    Try this:

    Sub a()
    Dim rCell As Range
    Dim finder As Variant
    finder = InputBox("Text to find")
    For Each rCell In Selection
    If InStr(rCell.Value, finder) > 0 Then rCell.Characters(InStr(rCell.Value, finder), Len(finder)).Font.ColorIndex = 4
    Next rCell
    End Sub
    Rule 1: Never merge cells
    Rule 2: See rule 1

    "Tomorrow I'm going to be famous. All I need is a tennis racket and a hat".

  3. #3
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: How to find a string and change it's color?

    Thanks sweep! That looks like it should work, but I'm getting an error
    when I try it (with some adjustments to fit the code I'm trying out here.) :

    
    Sub a()
    Dim rCell As Range
    Dim lRow As Long
    Dim finder As Variant
    finder = "End"
    
    For lRow = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
    rCell = Cells(lRow, 1).Value
    
    If InStr(rCell.Value, finder) > 0 Then
        rCell.Characters(InStr(rCell.Value, finder), Len(finder)).Font.ColorIndex = 4
    End If
    
    Next lRow
    
    End Sub
    I'm getting the error: "Object variable or With block variable not set" on the line: rCell = Cells(lRow, 1).Value.

    If I Dim rCell as a variant, I get the error "Object required" on the line:
    "If InStr(rCell.Value, finder) > 0 Then"

    Am I making some glaring mistake somewhere here please?

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,444

    Re: How to find a string and change it's color?

    rCell is a range object and requires the Set command to make the assignment

    set rCell = Cells(lRow, 1)
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Contributor
    Join Date
    01-13-2009
    Location
    Mumbai
    MS-Off Ver
    Excel 2003, 2007
    Posts
    168

    Re: How to find a string and change it's color?

    Hi,

    The mistake that you are making is that you are declaring rCell as range and then you are using it as a string..

    Try this code instead

    Sub a()
    Dim rCell As Range
    Dim lRow As Long
    Dim finder As Variant
    finder = "End"
    
    For lRow = Cells(Rows.Count, "A").End(xlUp).Row To 1 Step -1
        Set rCell = Range(Cells(lRow, 1), Cells(lRow, 1))
        
        If InStr(rCell.Value, finder) > 0 Then
            rCell.Characters(InStr(rCell.Value, finder), Len(finder)).Font.ColorIndex = 4
        End If
    
    Next lRow
    
    End Sub
    HTH,
    Vaibhav
    Last edited by c.vaibhav; 05-13-2009 at 02:30 AM.

  6. #6
    Registered User
    Join Date
    04-08-2009
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    85

    Re: How to find a string and change it's color?

    Thanks for that guys! Works fine now
    I assume btw that it will work similarly if I make 'finder' an
    array of values, so I can change the color of multiple words using
    this code? (I'm doing this as an exercise to color my code before
    printing it out.) I'll test that out later anyway as it's nearly
    midnight here now

+ 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