在Excel上为文本值创build有条件的格式化macros

很多时候我需要在我的Excel工作表上创build有条件的格式化规则,而不总是在同一个范围内,根据写入的内容设置文本的颜色格式。

最常见的情况是将文本“有效”绿色和粗体的范围内的所有单元格,以及“无效”的红色和粗体。

我试图使用“开发人员”选项卡上的“录制macros”function来创build此macros,但是它不起作用,代码为空。

由于我对VBA没有什么知识,所以我想知道是否有人能够帮助我创build这个macros。

定义:

  • 没有固定的范围,它需要捕获选定的范围;
  • 基于文本的格式,如果“生效”绿色和粗体,如果“不生效”红色和粗体。
  • 只有一张纸。

[解决了]

Sub EffectiveNot() ' ' EffectiveNot Macro ' Dim rStart As Range Set rStart = Selection Selection.FormatConditions.Add Type:=xlTextString, String:="Effective", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Bold = True .Italic = False .Color = -11489280 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False Selection.FormatConditions.Add Type:=xlTextString, String:="Not effective", _ TextOperator:=xlContains Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority With Selection.FormatConditions(1).Font .Bold = True .Italic = False .Color = -16776961 .TintAndShade = 0 End With Selection.FormatConditions(1).StopIfTrue = False End Sub 

你有没有机会检查你的ThisWorkbook -Module? macroslogging器每天添加一个新的空模块,然后转储代码

这基本上是macroslogging器提出来,在我清理了一下之后。 随意将Selection交换到更适合您使用的范围对象。

 Option Explicit Sub format() With Selection With .FormatConditions .Delete With .Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""Not Effective""") With .Font .Color = vbRed .Bold = True End With .StopIfTrue = False End With With .Add(Type:=xlCellValue, Operator:=xlEqual, Formula1:="=""Effective""") With .Font .Color = vbGreen .Bold = True End With .StopIfTrue = False End With End With End With End Sub