简体   繁体   中英

VBA Font Conditional Formatting in Excel

So I thought it would be relatively simple to format a grouping of cells as I create them in VBA though I guess I was wrong. My aim is to black out a group of cells unless a certain value is entered into a different cell. The issue I am having is that the formatting for the font isn't working for some reason. I have even gone as far as copying the VBA from a macro that does what I want but it always trips up at the font section. The fill color is black as it should be but the font section throws an error: "Application-defined or object-defined error" Below is the code generated by the macro recorder (With my formula added in) that will actually fail if I try to run it after recording it successfully:

Range(Cells(35, 9 + (11 * (Range("OptionCount").Value + 1))), Cells(40, 9 + (11 * (Range("OptionCount").Value + 1)))).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=$" & Ltrs & "$33 <>" & """Custom" & Range("OptionCount").Value & """"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
End With
With Selection.FormatConditions(1).Font 'Error Here
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
End With

The only thing I can think of causing an issue may be that there's more than 3 conditional formats on the cells already, but if that was the case I would think that the fill color wouldn't work either, but it does.

I have a solution for your original problem (I hope) but encountered another problem. These 2 subs do what you ask, but need some tweaking to adapt to your specific situation. The sub del_format will delete all formatting that exists in the given range, so be carefull with that.

I initially created a rule for the whole range, but that reacted only to some of the cells. This sub loops all the cells in the range and applies the formatting to each cell. Problem is, in my testing, it skips the 2nd row of each column. I honestly don't know why. Perhaps some excel Guru can react?

Option Explicit

Sub jzz()
Dim c As Range
Dim testRange As Range
Dim fCon As FormatCondition

Set testRange = Range("D1:H20")

Call del_format(testRange)

For Each c In testRange.Cells
    Set fCon = c.FormatConditions.Add(Type:=xlExpression, Formula1:="=$B$1<>33")
    With fCon.Interior
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
    End With
    With fCon.Font
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
    End With
    Set fCon = Nothing
Next c
End Sub
Sub del_format(r As Range)

Dim a As Object
Dim c As Range
For Each c In r.Cells
    For Each a In c.FormatConditions
        a.Delete
    Next a
Next c

Hope you don't suffer the bug (is it a bug?) that I encountered here.

EDIT: the problem seems some graphical issue. If I run the sub and scroll down and up again, the cells are allright.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM