Question

I want to add conditional formatting on a range depending on some condition.

I want to to format the Color, Size , Bold etc properties. I am able to modify Color, Bold properties but when i try to modify Size property it throws an exception "Unable to set the Size property of the Font class".

Can anyone help how can i set the Size property of Conditional Formatting object.

It is not possible to set subscript or superscript property also.

Note : These properties are not Read-Only also.

            FormatCondition format =(FormatCondition)( targetSheet.get_Range("A1:A10",
            Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression, XlFormatConditionOperator.xlGreater,
            "100", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing));

            format.Font.Bold = true;
            format.Font.Color = 0x000000FF;
            format.Font.Size = 14;
            format.Font.Subscript = true;

To make clear the actual usecase lets take an example where i have data of 10*10 in excel. This 10*10 is a single Range with its own format. For E.g A1 to J10. Now if i select any cell of this range then its corresponding Rows and Columns should get a Conditional Formating and it would have its own formats. This includes different fill color, different font size, change in border etc. Such as if i select cell D4 then Range A4:J4 and D1:D10 will have conditional formats applied.This can be done by applying formats on these both range and choosing type as Expression and its formula as true. Now if i select any other cell then the formats of A4:J4 and D1:D10 cells should be reverted back and the rows and colummn for the currently selected cell should be highlighted.

We can change the format such as only the Color or pattern. But its not possible to set the Size. Can anyone explain me why is it so. Changing the size from User Interface is possible. i.e using the Format option of conditional formatting. Using that the font size,color etc of the cells which satisfies the condition can be changed.

Things which are possible from UI should be possible from code also.

Have share an image to get a view: http://imgur.com/bemI9

Was it helpful?

Solution

Try the following (you can change the color values and add additional properties for font size, color, etc.):

Dim fontSize As Variant
Dim fontType As Variant
Dim fontBold As Variant
Dim cellIn As Variant

Sub setVars()

    With Range("A1")
        fontSize = .Font.Size
        fontType = .Font.Name
        fontBold = .Font.Bold
        cellIn = .Interior.Color
    End With


End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Application.ScreenUpdating = False
    Application.EnableEvents = False

    Dim r As Range
    Set r = Range("A1:J10")

    With r
        .Font.Size = fontSize
        .Font.Name = fontType
        .Font.Bold = fontBold
        .Interior.Color = cellIn
    End With

    With r.Rows(Target.Row)
        .Interior.ColorIndex = 5
        .Font.Bold = True
    End With

    With r.Columns(Target.Column)
        .Interior.ColorIndex = 5
        .Font.Bold = True
    End With

    Application.ScreenUpdating = True
    Application.EnableEvents = True

End Sub

OTHER TIPS

My research said: "You can't change the font in Conditional Formatting".

I would do it like that in VBA:

For Each cell In Range("A1:A10")
    With cell
          .Font.Bold = true
          .Font.Color = 0x000000FF
          .Font.Size = 14
    End With
Next 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top