使用Excel VBA代码进行条件格式化

我有称为DistinationRange的Range对象,它包含对范围B3:H63的引用

我想dynamic使用Excel VBA代码应用以下两个条件格式。 (因为范围不一直都是一样的)

  1. 如果单元格列D为空白,则不应应用格式设置(需要在此处使用Stop If True)
  2. 如果E列的单元格中的值小于F列单元格中的值,那么整行应该有绿色背景。

我尝试了很多使用录音,但它没有正确录音。

请帮助。

这将使你得到一个简单的答案,但是你可以扩展你将如何知道需要比较哪些列(在这种情况下是B和C)以及初始范围(本例中是A1:D5 )将会? 然后我可以尝试提供一个更完整的答案。

 Sub setCondFormat() Range("B3").Select With Range("B3:H63") .FormatConditions.Add Type:=xlExpression, Formula1:= _ "=IF($D3="""",FALSE,IF($F3>=$E3,TRUE,FALSE))" With .FormatConditions(.FormatConditions.Count) .SetFirstPriority With .Interior .PatternColorIndex = xlAutomatic .Color = 5287936 .TintAndShade = 0 End With End With End With End Sub 

注意:这是在Excel 2010中testing的。

编辑:根据评论更新代码。

我想我刚刚发现了一种使用VBA以预期方式应用重叠条件的方法。 经过几个小时的尝试不同的方法,我发现什么工作是改变了“适用于”范围的条件格式规则,每一个创build后!

这是我的工作示例:

 Sub ResetFormatting() ' ---------------------------------------------------------------------------------------- ' Written by..: Julius Getz Mørk ' Purpose.....: If conditional formatting ranges are broken it might cause a huge increase ' in duplicated formatting rules that in turn will significantly slow down ' the spreadsheet. ' This macro is designed to reset all formatting rules to default. ' ---------------------------------------------------------------------------------------- On Error GoTo ErrHandler ' Make sure we are positioned in the correct sheet WS_PROMO.Select ' Disable Events Application.EnableEvents = False ' Delete all conditional formatting rules in sheet Cells.FormatConditions.Delete ' CREATE ALL THE CONDITIONAL FORMATTING RULES: ' (1) Make negative values red With Cells(1, 1).FormatConditions.add(xlCellValue, xlLess, "=0") .Font.Color = -16776961 .StopIfTrue = False End With ' (2) Highlight defined good margin as green values With Cells(1, 1).FormatConditions.add(xlCellValue, xlGreater, "=CP_HIGH_MARGIN_DEFINITION") .Font.Color = -16744448 .StopIfTrue = False End With ' (3) Make article strategy "D" red With Cells(1, 1).FormatConditions.add(xlCellValue, xlEqual, "=""D""") .Font.Bold = True .Font.Color = -16776961 .StopIfTrue = False End With ' (4) Make article strategy "A" blue With Cells(1, 1).FormatConditions.add(xlCellValue, xlEqual, "=""A""") .Font.Bold = True .Font.Color = -10092544 .StopIfTrue = False End With ' (5) Make article strategy "W" green With Cells(1, 1).FormatConditions.add(xlCellValue, xlEqual, "=""W""") .Font.Bold = True .Font.Color = -16744448 .StopIfTrue = False End With ' (6) Show special cost in bold green font With Cells(1, 1).FormatConditions.add(xlCellValue, xlNotEqual, "=0") .Font.Bold = True .Font.Color = -16744448 .StopIfTrue = False End With ' (7) Highlight duplicate heading names. There can be none. With Cells(1, 1).FormatConditions.AddUniqueValues .DupeUnique = xlDuplicate .Font.Color = -16383844 .Interior.Color = 13551615 .StopIfTrue = False End With ' (8) Make heading rows bold with yellow background With Cells(1, 1).FormatConditions.add(Type:=xlExpression, Formula1:="=IF($B8=""H"";TRUE;FALSE)") .Font.Bold = True .Interior.Color = 13434879 .StopIfTrue = False End With ' Modify the "Applies To" ranges Cells.FormatConditions(1).ModifyAppliesToRange Range("O8:P507") Cells.FormatConditions(2).ModifyAppliesToRange Range("O8:O507") Cells.FormatConditions(3).ModifyAppliesToRange Range("B8:B507") Cells.FormatConditions(4).ModifyAppliesToRange Range("B8:B507") Cells.FormatConditions(5).ModifyAppliesToRange Range("B8:B507") Cells.FormatConditions(6).ModifyAppliesToRange Range("E8:E507") Cells.FormatConditions(7).ModifyAppliesToRange Range("A7:AE7") Cells.FormatConditions(8).ModifyAppliesToRange Range("B8:L507") ErrHandler: Application.EnableEvents = False End Sub