+ Reply to Thread
Results 1 to 3 of 3

Auto change colour of cell when text / letter entered into cell

Hybrid View

  1. #1
    Registered User
    Join Date
    12-22-2007
    Posts
    10

    Auto change colour of cell when text / letter entered into cell

    Hi guys,

    Again I found another very helpful bit while trying to update my report.

    We had a sheet for staff leave, and I needed more than 3 options for conditional formatting, so I found some VB code and modified it as below.

    This gave me multipul options that did multipul colors. When I type the letter the cell turns the corresponding color;

    W = Yellow
    S = Green
    SN = Sky Blue
    B = Purple
    P = Light purple
    A = Dark Yellow
    BR = Grey
    J = Dark Orange
    V = Light blue

    -> Right click on worksheet and choose 'view code' - past the below anad modify cell ref's as appropriate


    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icolor As Integer
    
        If Not Intersect(Target, Range("B5:AF286")) Is Nothing Then
            Select Case Target
                Case "W"
                    icolor = 6
                Case "S"
                    icolor = 4
                Case "SN"
                    icolor = 33
                Case "B"
                    icolor = 7
                Case "P"
                    icolor = 39
                Case "A"
                    icolor = 44
                Case "BR"
                    icolor = 15
                Case "V"
                    icolor = 34
                Case "J"
                    icolor = 46
                Case "w"
                    icolor = 6
                Case "s"
                    icolor = 4
                Case "sn"
                    icolor = 33
                Case "b"
                    icolor = 7
                Case "p"
                    icolor = 39
                Case "a"
                    icolor = 44
                Case "br"
                    icolor = 15
                Case "v"
                    icolor = 34
                Case "j"
                    icolor = 46
                Case "W.5"
                    icolor = 6
                Case "S"
                    icolor = 4
                Case "SN"
                    icolor = 33
                Case "B.5"
                    icolor = 7
                Case "P.5"
                    icolor = 39
                Case "A.5"
                    icolor = 44
                Case "BR.5"
                    icolor = 15
                Case "V.5"
                    icolor = 34
                Case "J.5"
                    icolor = 46
                Case "w.5"
                    icolor = 6
                Case "s.5"
                    icolor = 4
                Case "sn.5"
                    icolor = 33
                Case "b.5"
                    icolor = 7
                Case "p.5"
                    icolor = 39
                Case "a.5"
                    icolor = 44
                Case "br.5"
                    icolor = 15
                Case "v.5"
                    icolor = 34
                Case "j.5"
                    icolor = 46
                Case Else
                    'Whatever
            End Select
            
            Target.Interior.ColorIndex = icolor
        End If
    
    End Sub
    Yay - works a treat
    Last edited by VBA Noob; 12-26-2007 at 05:34 AM.

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    You can shorten by using the Ucase function

    More examples of select case here

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icolor As Integer
    
        If Not Intersect(Target, Range("B5:AF286")) Is Nothing Then
            Select Case UCase(Target)
                Case "W", "W.5"
                    icolor = 6
                Case "S", "S.5"
                    icolor = 4
                Case "SN", "SN.5"
                    icolor = 33
                Case "B", "B.5"
                    icolor = 7
                Case "P", "P.5"
                    icolor = 39
                Case "A", "A.5"
                    icolor = 44
                Case "BR", "BR.5"
                    icolor = 15
                Case "V", "V.5"
                    icolor = 34
                Case "J", "J.5"
                    icolor = 46
                Case Else
                    'Whatever
            End Select
            
            Target.Interior.ColorIndex = icolor
        End If
    
    End Sub
    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Registered User
    Join Date
    12-22-2007
    Posts
    10
    Cheers,

    I was wondering after I had finished if I could shorten this by using comma's on the same line as you have suggested - "W", "W.5"

    I had no idea abaout Ucase though to cover both upper & lower case.

    Thanks for hte tip that will tidy up the script nicley

+ 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