+ Reply to Thread
Results 1 to 42 of 42

VBA Conditional Formatting - Change Font Color across row per 1st column (6 colors)

  1. #1
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    VBA Conditional Formatting - Change Font Color across row per 1st column (6 colors)

    I need to find VBA code for:
    -- Changing the font color of part of a row (within a named range), based on the value of the cell in the first column of the row (within the range)
    -- I need at least 6 colors, each with a different criterion, so regular conditional formatting won’t work (I have Excel 2003).

    The file is a calendar for managing the schedules of employees of a small department, formatted as the attached fragment of the whole. The first column within each block of cells (block = a single date on the calendar) is a pull-down list of the initials of the employees. As I select a different employee or change that first cell, I want the font color to change for that cell and the cells to the right within that date block.

    I think I’ll need a different section of code for each day-of-the-week range, which I’ve named “Sunday” “Monday”, “Tuesday”, etc.

    I’d prefer to reference the employees by the named cell which contains their initials, i.e. “Name1” “Name3” “Name3”, etc, rather than the actual initials (“KG” “SB” etc), since employees may change.

    I know the color numbers for what I want:
    Name1 – 1
    Name2 – 3
    Name3 – 4
    Name4 – 7
    Name5 – 5
    Name6 – 46

    I’ve searched and tried various solutions from the web, but none seem to work quite right. The ones I’ve tried result in error messages, or the color for a particular cell gets set and then doesn’t change, even if I change the value.

    Thanks!

    John
    Attached Files Attached Files
    Last edited by jds217; 04-09-2009 at 09:22 PM. Reason: Solved

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    It is easily done if we knew a few more specifics. Can you attach one more example with the colors manually set for 1 or 2 names?

    I would code it to trigger off the change event for columns A, F, M, and T. I am just not sure what colors would go where. I am guessing maybe replace the yellow color?

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

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Try this code, note it works by checking the cells containing data validation
    Please Login or Register  to view this content.
    Copy the code
    Select the worksheet in which you want the code to run
    Right click on the sheet tab and choose View Code, to open the Visual Basic Editor.
    Where the cursor is flashing, choose Edit | Paste
    Attached Files Attached Files
    Last edited by royUK; 04-09-2009 at 02:17 PM.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    I put in a macro that gets you started.

    I only did Sunday,Monday,Tuesday and Name1,Name2,Name3. You can finish the rest, and modify it accordingly.

    Look in Worksheet_Change() in the Sheet1 Module.
    Attached Files Attached Files

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

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Missed the bit about referencing the cell not the initials
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Roy (and all) --

    Thanks so much! That helps a great deal, better than anything else I've managed to try.

    Two things I still need help with:

    1) Some of the columns in the date block do not use data validation. Some are the result of formulas, and some are just input from the user.

    2) It seems that if I have data continuous across several date blocks (i.e., all the columns have something in them), if I change one of the names, everything contiguous to the right changes color, even in adjacent date blocks.

    I guess that's why I was thinking I needed a solution that addressed particular named ranges.

    Thanks so much!

  7. #7
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    P.S. My last post was particularly in reference to Roy's solution.

    I'm also wondering if there's a way I can change the font color of JUST the employee initials column, and then set a regular conditional format condition for the other columns to COPY the format color from whatever is in the first column?

    Would that be simpler?

    Thanks!

    John

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

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    You need an empty colmn between blocks as in the example that you posted

  9. #9
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    I guess you didn't look at the workbook I upladed.
    So, I'll post my partial solution here.
    I've improved it so that it takes the color from Name1, Name2, and copies across the block.

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy --

    Thank you! Yes, I did look at yours as well, but it changed the interior of the cell, rather than the font. I'll give your code a try as well and see if I can change it for font color.

    Thank you!

    John

  11. #11
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    The only other problem I'm running into is that the working copy of the sheet is protected, so users can only enter data in the unlocked cells. When I try use it on a protected sheet, I get 'Runtime error 1004' saying that it can't be done on a protected sheet.

  12. #12
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    If you use a macro to protect the sheet, you can add the "userinterfaceonly:=True" capability. Then your macros can change protected cells, but the users can't.

    this code protects all worksheets so that your macros can change protected cells, but the users can't.
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy --

    I pasted the code and changed Interior.ColorIndex to Font.ColorIndex. I get the message:

    Run-time error '1004'
    Method 'Range' of object '_Worksheet' failed.

    Thanks for any assistance you can provide.

    John

  14. #14
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    What line did it crash on?

  15. #15
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    scratch this message.

  16. #16
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    What version of Excel are you using?

  17. #17
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy --

    It indicates the crash at:
    Case Range("Name1").Value

    Thanks!

  18. #18
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Excel 2003

  19. #19
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    That's indicating that there is no Range("Name1") on the activeworksheet. Did you accidentally delete Range Name "Name1"?

  20. #20
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy --

    Sorry about that ... obviously there are more details that are relevant than I realized.

    The range 'Name1' is on another sheet in the same workbook.

    John

  21. #21
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    put lines at the top:

    Please Login or Register  to view this content.
    where xxxx is the name of sheet where "Name1" is.

    and change the line to:
    Please Login or Register  to view this content.
    and obviously all the rest of the relevent lines.

  22. #22
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Well, perhaps we're getting there ...

    Now I get an error message:

    Unable to set the ColorIndex property of the Font class, and it stops executing at:

    Cells(Target.Row, lCol).Font.ColorIndex = lColor

    Also, these seems to mess somehow with my macro for clearing the unprotected cells of the calendar (providing a clean copy for a new month):

    That macro reads:

    Please Login or Register  to view this content.
    Last edited by jds217; 04-09-2009 at 05:03 PM. Reason: adding code tags

  23. #23
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    userinterfaceonly should be TRUE, so that it only stops the users from changing locked cells.

    Scratch this message, I mis read your message.
    Last edited by foxguy; 04-09-2009 at 04:53 PM. Reason: mis read your message.

  24. #24
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    I switch it FALSE for the procedure for clearing the calendar -- otherwise it clears my formulas and everything.

    Then I switch it back to TRUE at the end of the clearing macro. At least I think that's what I'm doing -- I know before I added that, I was wiping out everything with this macro.

  25. #25
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    jds,

    Please edit your post to add code tags.
    Entia non sunt multiplicanda sine necessitate

  26. #26
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Ok, I think I have it.

    At the top of the macro put:

    Please Login or Register  to view this content.

  27. #27
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    That's a neat trick for clearing unlocked cells. It seems a little dangerous to me, but I'll have to remember it.

  28. #28
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Should I put that bit of code at the top of the macro, below, the opening, like this:

    Please Login or Register  to view this content.
    Doing it that way gets me a Run-time error 404 "Object required"

    Also, do you know of a safer, better way to clear the data input/unlocked cells in my calendar?

    Thanks so very much

  29. #29
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    I meant in the "Worksheet_Change() macro.

    That way when your EmptyUnlocked() macro runs, my macro won't interfere with it.

    There is another solution which would cover any future changes.

    Please Login or Register  to view this content.
    Then NO other macros will run while your's is running.

  30. #30
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Scratch - wrong thread

  31. #31
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    OK, yes, I moved the code to the Worksheet_Change() macro, and now everything is executing without any errors.

    However, the color-coding isn't working. I get different colors for the first column for the first 3 days, and just in the first week, but it doesn't carry across the other columns in the date block.

    I'm attaching the actual spreadsheet, if that would make it clearer.

    And thank you very, very much for all the help!

    John
    Attached Files Attached Files

  32. #32
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Quote Originally Posted by jds217 View Post
    Also, do you know of a safer, better way to clear the data input/unlocked cells in my calendar?
    I think I would cycle through all the cells specifically looking for unlocked cells.

    Please Login or Register  to view this content.
    this would probably take a little longer, but it's a lot safer. And the speed probably wouldn't be noticable.

  33. #33
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    One other thing I've discovered ... when I first open the file, anytime I try to change an employee, I get an error message:

    Unable to set the ColorIndex property of the Font class.

    However, once I run the macro to clear the calendar, then I don't get that error message anymore.

    Still having the same trouble with the colors.

  34. #34
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    You need to set the font color in Names on Sheet1. That's where the Macro picks up the color to copy across the data block.

    Also, If you want it to change the font color in "Sunday", "Monday", etc.
    change the line from
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    I changed it in the attached file.
    Look at the range Name "Names" on sheet1. Just change the font color to whatever you want for each person, and it will update the color on the calendar sheet the next time you select that name.
    Attached Files Attached Files

  35. #35
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Quote Originally Posted by jds217 View Post
    One other thing I've discovered ... when I first open the file, anytime I try to change an employee, I get an error message:

    Unable to set the ColorIndex property of the Font class.

    However, once I run the macro to clear the calendar, then I don't get that error message anymore.
    the userinterfaceonly property can not be stored when the workbook closes. It has to be set EVERY time you open the workbook.

    You need to run this when the workbook opens

    Please Login or Register  to view this content.
    Then in emptyUnlocked you can replace
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.

  36. #36
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Yes, beautiful and brilliant!!!! Thank you!

    One last thing I need help with ... the LAST column in each day, Monday-Saturday (Sunday is different, requires less data) -- columns S, Z, AG, AN, AU, BB -- is a user input cell, not a drop-down list, and the color formatting isn't working right for just those cells.

    Thanks!!!!

    John

  37. #37
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    And trying to figure out how to set up a macro to on opening ... thought I had it, but I get a "Compile Error: Named argument not found" on the

    Please Login or Register  to view this content.
    line

  38. #38
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy --

    Thank you so much ... I've worked with the file, and got the opening macro to work ... very happy about that. I'm going to see if I can get that last column to color format correctly.

  39. #39
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy ... YES ... I tried to decipher the code, and figured out how to fix that last column. Perfect solution!!! Thanks for all your help!!

    John

  40. #40
    Forum Expert
    Join Date
    03-31-2009
    Location
    Barstow, Ca
    MS-Off Ver
    Excel 2002 & 2007
    Posts
    2,164

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Just got back.

    I am curious as to that compile error. Did you figure out what argument was not found?

    I assume you figured out to put Sub Workbook_Open() in the ThisWorkbook module.

    I assume you figured out the line should be :
    Please Login or Register  to view this content.
    I guess I'm not a good counter

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

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    The amended code that I posted is triggered when the initial is selected in the Data validation list. It colours each cell in the row of that day.I used xlToRight because your blocks did not all contain the same number of columns. If you have a fixed number of columns in each day then the code could be amended easily. The only necessity is an empty column between days and it will work without changing on each day that you add.

    Code to colour the initials plus next four columns
    Please Login or Register  to view this content.
    What is it not doing that you outlined in the first post? You don't need loops hich will only slow down the code.
    Attached Files Attached Files
    Last edited by royUK; 04-10-2009 at 04:12 AM.

  42. #42
    Registered User
    Join Date
    11-06-2005
    Posts
    23

    Re: VBA Conditional Formatting - Change Font Color across row per 1st column (6 color

    Foxguy -- Yes, I tried to examine the code and figure out which part was the "number of columns to change", and just changed it to 6, and it worked perfectly. Thank you very much. I learned a few things in the process, and now am committed to getting a book from the library on learning more VBA code so I can take ideas I find online and tweak them.

    Roy, thank you for your input as well. I think part of the difficulties I was having was that my original spreadsheet was more complicated than the fragment I'd posted, and I didn't realize how those differences were affecting the results. With much (greatly appreciated) correspondence between Foxguy and myself, he was able to help me hammer out a solution that worked.

    John

+ 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