如何在相同的范围内使用VBA进行条件格式化

我正在尝试使用VBA在类似的范围上执行条件格式。 我相信我的代码中的错误与优先级有关,但我不知道它是什么。 我试图格式化本质上相同的一组单元格。 如果列CI包含文本“TIES MATERIAL”,那么它应该在下面的情况下将CU:DD的列格式化为该特定行的白色。 如果该列不包含文本string,并且该值已从其原始值更改,那么应将单元格更改为红色。

这里是我使它变成白色的代码:

Private Sub white_format() 'This section adds the formatting condition of white cells to the cells that are changed by the PEMCON ActiveWorkbook.Sheets("Material").Activate Dim lRow As Integer Dim lCol As Integer lRow = ActiveWorkbook.Sheets("Material").Range("A2").End(xlDown).Row lCol = ActiveWorkbook.Sheets("Material").Range("A2").End(xlToRight).Column firstCell = ActiveWorkbook.Sheets("Material").Range("CU3").Address(False, False) lastCell = ActiveWorkbook.Sheets("Material").Cells(lRow, lCol).Address(False, False) Debug.Print "firstCell: " & firstCell Debug.Print "lastHeaderCell: " & lastHeaderCell Debug.Print "colCount: " & colCount 'Defines the array of the CU3 to the last used cell and it checks to see if the coorisponding cell in column CI has TIES MATERIAL in it ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions.Add Type:=xlExpression, Formula1:="=$CI3=""TIES MATERIAL""" ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).SetFirstPriority With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 16777215 'this is the color white .TintAndShade = 0 End With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).StopIfTrue = True End Sub 

这里是我使它变成红色的代码:

 Private Sub Red_Format() Dim lRow As Integer Dim lCol As Integer lRow = ActiveWorkbook.Sheets("Material").Range("A2").End(xlDown).Row lCol = ActiveWorkbook.Sheets("Material").Range("A2").End(xlToRight).Column firstCell = ActiveWorkbook.Sheets("Material").Range("CU2").Address(False, False) lastCell = ActiveWorkbook.Sheets("Material").Cells(lRow, lCol).Address(False, False) formatRange = ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell) lastHeaderCell = ActiveWorkbook.Sheets("Material").Cells(2, lCol).Address(False, False) colCount = ActiveWorkbook.Sheets("Material").Range(firstCell, lastHeaderCell).Columns.Count 'Defines the array of the CU2 to the last used cell and adds the formatting to turn it red if it has been altered. ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions.Add Type:=xlExpression, Formula1:="=OFFSET($A$1,ROW()-1,COLUMN()-1)<>OFFSET($A$1,ROW()-1,COLUMN()+" & colCount & ")" 'ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).SetFirstPriority With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .Color = 255 .TintAndShade = 0 End With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).StopIfTrue = False End Sub 

这是当我调用white_format时条件格式的样子,然后在相同的子例程中调用red_format条件格式结果

公式显示正确,但颜色在他们需要在相反的部分。我做错了什么? 我也知道我的代码不是最有效的,它可以/应该是。 我还能怎么重写呢?

 Formula1:="=$CI3=""TIES MATERIAL""" 

“如果列CI包含文本”TIES MATERIAL“,那么它应该将单元格格式化为白色”

要做到这一点,你的格式条件应该是:

 Formula1:="=ISNUMBER(SEARCH(""TIES MATERIAL"", $CI" & firstCell.Row & "))" 

至于第二个条件,我还没有得到你想要实现的想法。 然而,这个公式可能是正确的,但是问题在于你错误地提到了它:

 With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(1).Interior 

因为这是第二个 FormatCondition ,所以你应该把它作为索引(2) 。 这就解释了为什么你实际上用红色覆盖第一个条件的格式,而第二个条件没有设置格式。

 With ActiveWorkbook.Sheets("Material").Range(firstCell, lastCell).FormatConditions(2).Interior ' ^^^ 

(这假设你的CFs在相同的范围内适用)。 如果没有,一个通常安全的方法是直接获取CF的引用并使用它:

 With myRange.formatConditions.Add(xlExpression, formula1) .Interior.ColorIndex = ... . etc... End With 

公式显示正确,但颜色是在他们需要在相反的部分。

这不完全正确。 您创build一个带有白色背景的CFR并将其设置为列表的顶部。 然后创build第二个,但在将其放到列表的顶部之前,将CFR列表的顶部更改为红色背景。 所以你有一个CFR过去是白色的背景,现在是红色的背景和第二个CFR没有背景。

我将假定红色CFR的公式是正确的。 其他人提出了一个非挥发性的变化。

 Option Explicit Private Sub white_red_CFRs() 'This section adds the formatting condition of white cells to the cells that are changed by the PEMCON Dim lRow As Long, lCol As Long, firstCell As String, lastCell As String Dim colCount As Long, lastHeaderCell As Long With ActiveWorkbook.Worksheets("Material") lRow = .Range("A2").End(xlDown).Row lCol = .Range("A2").End(xlToRight).Column firstCell = .Range("CU3").Address(False, False) lastCell = .Cells(lRow, lCol).Address(False, False) With .Range(firstCell, lastCell) .FormatConditions.Delete With .FormatConditions.Add(Type:=xlExpression, Formula1:="=$CI3=""TIES MATERIAL""") .Interior.Color = 16777215 'this is the color white .SetFirstPriority .StopIfTrue = True End With With .FormatConditions.Add(Type:=xlExpression, Formula1:="=OFFSET($A$1,ROW()-1,COLUMN()-1)<>OFFSET($A$1,ROW()-1,COLUMN()+" & colCount & ")") .Interior.Color = 255 'this is the color red .StopIfTrue = True End With End With End With End Sub 

当你logging一个CFR时,把你的行为转换成代码的Excel的一部分不知道已经存在多less个CFR,所以它使每个CFR都成为第一个CFR,所以它可以继续进行configuration,并将新的CFR称为.FormatConditions(1) 。 在将第二个CFR设置为顶部(1)CFR之前,您将格式configuration设置为.FormatConditions(1) 。 我宁愿使用With .Add方法。

好。 再次,要理解你试图达到的目标,以及在没有提供testing材料的情况下出了什么问题(而且那些专栏已经到了非常正确的地方,我不得不简化一下),这有点困难。我。

当列CI读取TIES MATERIAL时,将单元格颜色格式化为白色时,您的规则公式“= $ CI3 =”“TIES MATERIAL”“”工作正常,但我只是想知道当它们不是白色时它们是什么颜色? 他们会因为另一个规则而变红吗? 然后他们的顺序是错误的,因为冲突的规则将采用列表中较高的规则来应用。 “如果真的停止”是针对2007年以前版本的Excel。

你可以在图像中看到一个错误,它来自你的代码:在“pipe理规则”对话框中,你会看到“白色规则”被标记为“没有设置格式”。 这是因为在这两个过程中都提到了FormatConditions(1)。 如果你先运行“白色规则”,“红色”,后者已经设置好,但同时打破了第一(最重要的),因为对范围的引用不匹配。

所以也许你想先运行“白色规则”,在创build“红色”时引用FormatConditions(2),但正如我所说的很难说。 🙂