+ Reply to Thread
Results 1 to 2 of 2

Change Color of cell based on specific letter / text

Hybrid View

  1. #1
    Registered User
    Join Date
    11-02-2012
    Location
    Dallas, TX
    MS-Off Ver
    Excel 2010
    Posts
    28

    Change Color of cell based on specific letter / text

    Can anyone give VBA help on creating a macro?

    To search the entire sheet and if any cell contains just an "A" turn the cell green. If it contains just an "R" turn the cell red.

    If the cells contain anything thing else, do nothing.
    For example "AM" then no change.

    I can do this is conditional formatting, but I need a macro for it.

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Change Color of cell based on specific letter / text

    Hi

    This could be simplified by making a loop to find A and color then find R and color using a subroutine.

    But thats a job for you.

    Enjoy

    
    Sub GreenRed()
    
    Dim rngFind As Range
        Dim strValueToPick As String
        Dim rngPicked As Range
        Dim rngLook As Range
        Dim strFirstAddress As String
        
    
        Cells.Select
        
        Set rngLook = Selection
        strValueToPick = "R"
        With rngLook
            Set rngFind = .Find(strValueToPick, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngPicked = rngFind
                Do
                    Set rngPicked = Union(rngPicked, rngFind)
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If Not rngPicked Is Nothing Then
            rngPicked.Select
    
    
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
        End If
        
        Cells.Select
        
        Set rngLook = Selection
        strValueToPick = "A"
        With rngLook
            Set rngFind = .Find(strValueToPick, .Cells(1, 1), LookIn:=xlValues, lookat:=xlWhole)
            If Not rngFind Is Nothing Then
                strFirstAddress = rngFind.Address
                Set rngPicked = rngFind
                Do
                    Set rngPicked = Union(rngPicked, rngFind)
                    Set rngFind = .FindNext(rngFind)
                Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
            End If
        End With
        
        If Not rngPicked Is Nothing Then
            rngPicked.Select
            
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 65280
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    End If
    
    End Sub

+ 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] Change Tab color based on text in a cell
    By Gcorn38055 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-08-2013, 11:22 AM
  2. Replies: 0
    Last Post: 03-12-2013, 04:29 PM
  3. Replies: 1
    Last Post: 05-10-2012, 11:51 PM
  4. [SOLVED] Change color of specific text within a cell?
    By avarga82@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-13-2006, 12:35 PM
  5. change text color based on adjacent cell text color
    By matthewst in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-01-2005, 03:49 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