Results 1 to 6 of 6

Thread: Excel, get conditinal formatting font color in VBA

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Resolved Excel, get conditinal formatting font color in VBA

    I have a spreadsheet in Excel that uses the conditional formatting formulas to set cell font colors when they exceed certain limits.
    e.g. cells > x have their font color set to Blue.

    I've been trying to read the individual cell font colors from within a VBA function, e.g.
    Code:
        Debug.Print rng.Font.Color
        Debug.Print rng.Font.FontStyle
        Debug.Print rng.Font.ColorIndex
        Debug.Print rng.Interior.Color
    But this doesn't seem to know about the conditional formatting changes,
    i.e. it returns the basic, default color info for the cell.

    Is the actual, current font color info, as seen by the user, discernable via code somewhere?

    Thanks, DaveBo
    Last edited by DaveBo; May 5th, 2005 at 03:53 PM.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Excel, get conditinal formatting font color in VBA

    The first thing you want to do, if you look for the coding of something in VBA, use the macro recorder to do it manually and look at the code created by this recording.
    I created a conditional format and the recorder gave me this:
    VB Code:
    1. Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlGreater, _
    2.         Formula1:="35"
    3.     With Selection.FormatConditions(1).Font
    4.         .Bold = True
    5.         .Italic = False
    6.     End With
    I hope you can take it from here
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Excel, get conditinal formatting font color in VBA

    Opus, Thanks, that's good info.
    It tells me about the formulas used, but it still doesn't tell me if the formulas have actually been tripped and any conditions set.
    i.e. it isn't telling me the current color of the cell.

    Thanks, DaveBo
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

  4. #4
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Excel, get conditinal formatting font color in VBA

    That's correct, however you can check using VBA if the condition is met, and you can check all possible formattings. It seems like a long way but it's possible.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Addicted Member
    Join Date
    Jan 2005
    Posts
    138

    Re: Excel, get conditinal formatting font color in VBA

    Quote Originally Posted by DaveBo
    Is the actual, current font color info, as seen by the user, discernable via code somewhere?

    Thanks, DaveBo
    I'm sure there is, but you could also check to see if the condition is met.
    e.g. If rgn > x then msgbox "This cell color has been changed"
    Implying that if the condition is true, the cell certainly has changed color.

    Just a workaround.

    p.s. I think that's what the previous post meant. Sorry..

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2001
    Location
    N42 29.340 W71 53.215
    Posts
    422

    Re: Excel, get conditinal formatting font color in VBA

    I tried actually reading and parsing the format condition formulas to recreate the calculations it would make and determine what it should have done,
    however ...
    the conditional formatting was set on a large range of cells, with relative addresses used, i.e. the formula for the target cell compared the cell contents in the same column as the target cell but at row 1 and the contents in the same row but at columns 3 & 4.
    Works fine in the sheet, but when reading the formulas in VBA the relative cell addresses were inconsistent, sometimes they pointed to the the proper columns & rows, other times they were the original formulas without the addresses adjusted for "relativity".

    Anyway, I gave up on that approach.
    "The wise man doesn't know all the answers, but he knows where to find them."
    VBForums is one place, but for the really important stuff ... here's a clue 1Tim3:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width