【发布时间】:2017-06-26 22:03:10
【问题描述】:
我正在尝试使用 VBA 对类似范围进行条件格式设置。我确信我的代码中的错误与优先级有关,但我无法弄清楚它是什么。我正在尝试格式化基本上相同的单元格组。如果 CI 列包含文本“TIES MATERIAL”,那么它应该将列 CU:DD 中的单元格格式化为该特定行的白色。如果该列不包含文本字符串并且值已从其原始值更改,则单元格应更改为红色。
这里是我让它变白的代码:
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 时,条件格式如下所示。
公式正确显示,但颜色位于它们需要位于的相反部分。我做错了什么?我也知道我的代码并不是它可能/应该是最有效的。不然怎么改?
【问题讨论】:
-
试试这个link,看看你能不能让它为你工作。
-
这是另一个conditional formatting link。你也可以看看这个link。我希望有些东西对你有用。
标签: vba excel conditional-formatting