+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: Using VB Code to change cell colour on back of formula

  1. #1
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Using VB Code to change cell colour on back of formula

    Hi

    I have a range of cells which populate the words Gold, Silver, Bronze or Red on the back of a LOOKUP formula which all works fine, I now need a VB Code to be able to change the cell colour depending on which word has been returned.

    I've attempted to adapt a previously used code however the cells are remaining white and I'm also getting a Runtime Error 13 which appears to be stopping other code within the sheet working as it should.

    This is the code I've tried using, can anyone see any obvious errors?

    Private Sub Worksheet_Calculate()
    Dim icol As Long
    Dim c As Range
    
    For Each c In Range("af4:af18")
    Application.ScreenUpdating = False
    
        Select Case UCase(c.Value)
            Case "GOLD": icol = 6
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 46
            Case "RED": icol = 3
            Case Else
            icol = 0
        End Select
        With c
            .Interior.ColorIndex = icol
            .Font.ColorIndex = icol
        End With
    Next c
    Application.ScreenUpdating = True
    
    End Sub
    Many thanks

  2. #2
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Using VB Code to change cell colour on back of formula

    Put your code in the worksheet Change module.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim icol As Long
        Dim c As Range
    
        For Each c In Range("af4:af18")
    
            Select Case UCase(c.Value)
            Case "GOLD": icol = 6
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 46
            Case "RED": icol = 3
            Case Else
                icol = 0
            End Select
            With c
                .Interior.ColorIndex = icol
                .Font.ColorIndex = icol
            End With
        Next c
    
    End Sub

    Can't you just use Conditional Formatting in Range("af4:af18")?
    Last edited by Marcol; 09-25-2010 at 05:50 AM.
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  3. #3
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: Using VB Code to change cell colour on back of formula

    Quote Originally Posted by Marcol View Post
    Put your code in the worksheet Change module.
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim icol As Long
        Dim c As Range
    
        For Each c In Range("af4:af18")
    
            Select Case UCase(c.Value)
            Case "GOLD": icol = 6
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 46
            Case "RED": icol = 3
            Case Else
                icol = 0
            End Select
            With c
                .Interior.ColorIndex = icol
                .Font.ColorIndex = icol
            End With
        Next c
    
    End Sub

    Can't you just use Conditional Formatting in Range("af4:af18")?
    Thanks for your reply

    I'm getting the following error when I try using the above code

    Compile error: Ambigous name detected: Worksheet_Changne

    In relation to the conditional formatting question, I should have said I onlt have access to Office 97-2003 at work which only has the option for 3 rules as you'll know

    Cheers

  4. #4
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Using VB Code to change cell colour on back of formula

    You've probably got two WorkSheet)Change event ptocedures, only one is allowed.

    Also, which version of Excel are you using? Update your profile with this information
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  5. #5
    Forum Guru Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    5,590

    Re: Using VB Code to change cell colour on back of formula

    You must have some code in the change event already.

    Right click the tab of the sheet in question then select > View Code

    Put the code in the resultant VBa window.
    If there is already something in the change event, then add your code to it.

    TIP
    Add the versions of Excel you are using to your profile, there are significant differences in them, and answers returned will be tailored to suit your version or earlier.

    A location would also help in predicting any possible regional variations. language, or time zone problems.
    If you need any more information, please feel free to ask.

    However, if this takes care of your needs, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED. It helps everybody! ....
    Also
    If you are satisfied by any members response to your problem please consider using the small Star icon botom left of thier post to show your appreciation.

  6. #6
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: Using VB Code to change cell colour on back of formula

    Quote Originally Posted by Marcol View Post
    Put the code in the resultant VBa window.
    If there is already something in the change event, then add your code to it..
    Might be a daft question but at what stage would I add the code? If these were the two codes where would the second code blend with the first and do I need to remove any code for them both to work

    Thanks for all your help

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icol As Long
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("E4:N18")) Is Nothing Then
    
    Application.ScreenUpdating = False
    
        Select Case UCase(Target.Value)
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 46
            Case "AMBER": icol = 44
            Case "RED": icol = 3
            Case "GOLD": icol = 6
            Case "CVP": icol = 1
            Case Else
            icol = 0
        End Select
        With Target
            .Interior.ColorIndex = icol
        End With
    End If
    
    Application.ScreenUpdating = True
    
    End Sub
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim icol As Long
        Dim c As Range
    
        For Each c In Range("af4:af18")
    
            Select Case UCase(c.Value)
            Case "GOLD": icol = 6
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 46
            Case "RED": icol = 3
            Case Else
                icol = 0
            End Select
            With c
                .Interior.ColorIndex = icol
                .Font.ColorIndex = icol
            End With
        Next c
    
    End Sub

  7. #7
    Forum Guru, retired Admin royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    25,639

    Re: Using VB Code to change cell colour on back of formula

    To best describe or illustrate your problem you would be better off attaching a dummy workbook, the workbook should contain the same structure and some dummy data of the same type as the type you have in your real workbook - so, if a cell contains numbers & letters in this format abc-123 then that should be reflected in the dummy workbook.

    If needed supply a before and after sheet in the workbook so the person helping you can see what you are trying to achieve.

    Doing this will ensure you get the result you need!
    Hope that helps.

    RoyUK
    --------
    If you are pleased with a member's answer then use the Star icon to rate it, if you are pleased enough to part with cash consider a donation to Children in Need

    For Excel Tips & Solutions, free examples and tutorials why not check out my downloads

    New members please read & follow the Forum Rules

    Remember to mark your questions Solved and rate the answer(s)

  8. #8
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: Using VB Code to change cell colour on back of formula

    Hi

    I've attached a dummy workbook as requested

    I'm almost there with this I think but I'm now getting a runtime error when I enter Gold, Silver etc in column E, the Scores in column AF are changing colour as I want although I also want to be able to see the text in column AF

    I've added text into cells E4 to E8 which all works fine, this in turn calculates a percentage score in column AD which then populates a status of either Gold, Silver, Bronze or Red in column AF which is the range of cells that I want to apply the code to change colour depending on the score

    Hope that all makes sense

    Many thanks in advance for any help
    Attached Files Attached Files

  9. #9
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Using VB Code to change cell colour on back of formula

    Private Sub Worksheet_Change(ByVal Target As Range)
      If Target.Cells.Count > 1 Then Exit Sub
       
      If Not Intersect(Target, Range("E4:N18")) Is Nothing Then
        On Error Resume Next
        With Target.Interior
          .ColorIndex = xlNone
          .ColorIndex = Val(Split("gold6silver15bronze46red3cvp1", lcase(Target.Text))(1))
          Target.Offset(, 27).Interior.ColorIndex = .ColorIndex
        End With
      End If
    End Sub



  10. #10
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: Using VB Code to change cell colour on back of formula

    Hi snb

    Thanks for the code, it works fine for the first part of what I'm trying to achieve however I also need the cells in column AF to change colour depending on the result of the LOOKUP formula

    How would I build on your code to do that please?

    Cheers

  11. #11
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    MSO2007 on WinXP/MSO2000 on Win7/winXP
    Posts
    2,180

    Re: Using VB Code to change cell colour on back of formula

    If Not Intersect(Target, Range("E4:N18", "Af4:af18")) Is Nothing Then
    ---
    Ben Van Johnson

  12. #12
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: Using VB Code to change cell colour on back of formula

    Quote Originally Posted by protonLeah View Post
    If Not Intersect(Target, Range("E4:N18", "Af4:af18")) Is Nothing Then
    Hi

    I've added the above code where I assume to be correct however the cells in AF4:AF18 remain unchanged.

    Can you see what is stopping the cells in AF4:AF18 changing?

    Many thanks

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim icol As Long
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Intersect(Target, Range("E4:N18", "Af4:af18")) Is Nothing Then
    
    Application.ScreenUpdating = False
    
        Select Case UCase(Target.Value)
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 45
            Case "AMBER": icol = 44
            Case "RED": icol = 3
            Case "GOLD": icol = 6
            Case "CVP": icol = 1
            Case Else
            icol = 0
        End Select
        With Target
            .Interior.ColorIndex = icol
        End With
    End If
    
    Application.ScreenUpdating = True
    
    End Sub

  13. #13
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Using VB Code to change cell colour on back of formula

    Hello Twaddy,

    This version of the macro works for me. Remove any Worksheet_Change() event code you have in the workbook. Follow the directions below to install the macro.

    NOTE: When a formula calculates, it does not trigger the Worksheet_Change event.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
      Dim icol As Long
      
        If Target.Cells.Count > 1 Then Exit Sub
        If Not Intersect(Target, Range("E4:N18", "AF4:AF18")) Is Nothing Then
    
           Select Case UCase(Target.Value)
            Case "SILVER": icol = 15
            Case "BRONZE": icol = 45
            Case "AMBER": icol = 44
            Case "RED": icol = 3
            Case "GOLD": icol = 6
            Case "CVP": icol = 1
            Case Else: icol = 0
          End Select
    
          Target.Interior.ColorIndex = icol
    
        End If
    
    End Sub

    How to Save a Worksheet Event Macro
    1. Copy the macro using CTRL+C keys.
    2. Open your Workbook and Right Click on the Worksheet's Name Tab for the Worksheet the macro will run on.
    3. Left Click on View Code in the pop up menu.
    4. Paste the macro code using CTRL+V
    5. Make any custom changes to the macro if needed at this time.
    6. Save the macro in your Workbook using CTRL+S
    Last edited by Leith Ross; 09-26-2010 at 07:36 PM.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  14. #14
    Registered User
    Join Date
    05-06-2008
    Location
    Chester, England
    MS-Off Ver
    Work: Office 97-03. Home: XP, Office 2010
    Posts
    35

    Re: Using VB Code to change cell colour on back of formula

    [QUOTE=Leith Ross;2389268]Hello Twaddy,

    NOTE: When a formula calculates, it does not trigger the Worksheet_Change event.

    QUOTE]

    Hi Leith

    Thanks for your reply

    So to clarify, it isn't possible to have a change event change the cell colour in e4:n18 based on what is typed into the cell AND that changes af4:af18 automatically depending on the outcome of the LOOKUP formula?

    Many thanks

    Twaddy

  15. #15
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Using VB Code to change cell colour on back of formula

    Hello Twaddy,

    The Worksheet_Change event is fired when either the user types in the cell and hits return or a user makes a selection from a Validation drop down list.

    However, when a cell's content is changed by a Formula, a cell link, a DDE conversation, a Forms drop down, or an ActiveX drop down then the event is NOT fired.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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.2.0