+ Reply to Thread
Results 1 to 20 of 20

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?

    Please Login or Register  to view this content.
    Many thanks

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

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

    Put your code in the worksheet Change module.
    Please Login or Register  to view this content.

    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 select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their 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.
    Please Login or Register  to view this content.

    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 Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    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
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

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

    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.

  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

    Please Login or Register  to view this content.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    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!

  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 Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

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

    Please Login or Register  to view this content.



  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
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,610

    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

    Please Login or Register  to view this content.

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

    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.

    Please Login or Register  to view this content.

    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, & 2010
    Posts
    23,258

    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.

  16. #16
    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

    Thanks very much for all your help and advise, as you can tell my knowledge around VB is almost non existent so your time is much appreciated.

    Looks like I'll have to look for an alternative solution, like getting my company to upgrade Office!

    Cheers

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

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

    Hi Twaddy

    It has been suggested several times in the replies to your thread, that you should post a sample workbook showing what you have so far.

    There is little or no reason why most of the replies you have recieved should not solve your problem, so I can only conclude that your actual workbook is a bit more involved than you are admitting.

    If you post a desensitised workbook then, just perhaps, we could not only solve your problem and save your company contributing to Mr William Henry "Bill" Gates IIIs' personal wealth and hey! your boss might even throw a token of gratitude towards yours.....

  18. #18
    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
    Hi Twaddy

    It has been suggested several times in the replies to your thread, that you should post a sample workbook showing what you have so far.

    There is little or no reason why most of the replies you have recieved should not solve your problem, so I can only conclude that your actual workbook is a bit more involved than you are admitting.

    If you post a desensitised workbook then, just perhaps, we could not only solve your problem and save your company contributing to Mr William Henry "Bill" Gates IIIs' personal wealth and hey! your boss might even throw a token of gratitude towards yours.....
    Hi

    I was only asked once to my knowledge to post a dummy workbook which I did in post No 8, perhaps I didn't post correctly or perhaps you missed it.

    My understanding from the replies are that a worksheet change event can not run alongside a worksheet calculate and also that a formula cannot trigger a worksheet change event.

    If I've misunderstood then I apologise.

    I really do appreciate all the help I've received

    Thanks



    Quote Originally Posted by Leith Ross View Post
    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.

  19. #19
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

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

    Hello Twaddy,

    I downloaded your workbook and had a look at what you were doing. After doing some tests, the results proved I was in error. The LOOKUP formula does trigger the Worksheet_Change event.

    I made corrections to the formulae in column "AD" to prevent the decision by zero error. An If statement is all that was needed.
    =IF(AB4 <>0,AC4/AB4,0)

    The macro code was askew and that has been corrected also. Here is the corrected code. All changes mentioned have been added to the attached workbook.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  20. #20
    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 Leith

    Thanks for taking the time to look at my litle problem, much appreciated.

    Something which I don't think I mentioned on previous posts or on the workbook was the bandings for differing grades in column AF which are as follows

    Red - 0% to 24%
    Bronze - 25% - 62%
    Silver 63% - 79%
    Gold 80% - 100%

    I've tweaked the formula in AF to reflect this as there isn't a banding for Amber. The formula appears to work ok and the correct grades populate depending on the score however I'm not sure if by doing so it's had a knock on effect to the code as the cell colours in AF aren't formatting correctly based on the text

    Would you mind taking a look to see if you can spot anything obvious?

    Thanks again

    I've attached an updated version of the worksheet with the amended formula
    Attached Files Attached Files

+ 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