Results 1 to 1 of 1

Thread: Conditional Formatting & Custom Functions

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    112

    Conditional Formatting & Custom Functions

    (Using Excel 2002 SP2)

    I'm having a problem using a custom function in a cell's conditional formatting, ONLY when I apply the formatting with VBA.

    The function is very simple. It returns a cell's height. The goal is to have formatting apply only when the cell directly above is not hidden.

    VB Code:
    1. Function CellHeight(rng As Range)
    2.     CellHeight = rng.Height
    3. End Function

    I want to apply the formatting to only certain cells that can only be determined at runtime.

    VB Code:
    1. Set rng = Cells(lngRows(i), j)
    2.  
    3. expression = "=IF(AND(CellHeight(" & rng.Offset(-1, 0).Address & ")>0,OR(AND(" & rng.Address & "<>"""",N(" & rng.Address & ")=0," & rng.Address & "=0),N(" & rng.Address & ")<>0)),TRUE,FALSE)"
    4.  
    5. rng.FormatConditions.Delete
    6. rng.FormatConditions.Add Type:=xlExpression, Formula1:=expression
    7. With rng.FormatConditions(1)
    8.     .Borders.Weight = rng.Borders.Weight
    9.     .Borders.LineStyle = rng.Borders.LineStyle
    10.     .Interior.ColorIndex = rng.Interior.ColorIndex
    11.     .Borders(xlTop).LineStyle = xlContinuous
    12.     .Borders(xlTop).Weight = 2
    13.     .Borders(xlBottom).LineStyle = xlContinuous
    14.     .Borders(xlBottom).Weight = 2
    15.     .Font.Bold = True
    16. End With

    The "expression" above translates to this:
    =IF(AND(CellHeight($B$11)>0,OR(AND($B$12<>"",N($B$12)=0,$B$12=0),N($B$12)<>0)),TRUE,FALSE)

    If I apply this formatting while working manually in the spreadsheet, it works fine. When I try to apply the formatting in VBA, execution halts. I don't get an error message or warning. The execution just stops at this line like it read an "END" statement.
    VB Code:
    1. rng.FormatConditions.Add Type:=xlExpression, Formula1:=expression

    I thought at first that the line was written incorrectly and it was some trick of fate that I didn't get a debug message when running. But if I change the condition and leave out my CellHeight function, the code runs just fine. So unless there's something that flew over my head investigating this, whenever I apply a conditional format using a cunstom function in VBA, execution halts without warning.

    Can anyone confirm this? Does anyone know a workaround, or a canned function to return the height of a row?

    Thanks,
    Last edited by mikeyc1204; Dec 19th, 2005 at 02:59 PM.

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