VBA条件格式不适用
我有一个例程,它循环遍历工作簿中所有工作表上的所有列,对所有列应用条件格式规则。规则保存在每一列上,我使用的规则很好,但在例程运行后颜色不会改变。然后,我可以进入工作表,选择条件格式,单击“编辑规则”>“确定”>“确定”,然后在工作表上更新格式,而无需更改有关规则的任何内容。要让规则真正更改工作表,我缺少什么VBA条件格式不适用,vba,conditional-formatting,Vba,Conditional Formatting,我有一个例程,它循环遍历工作簿中所有工作表上的所有列,对所有列应用条件格式规则。规则保存在每一列上,我使用的规则很好,但在例程运行后颜色不会改变。然后,我可以进入工作表,选择条件格式,单击“编辑规则”>“确定”>“确定”,然后在工作表上更新格式,而无需更改有关规则的任何内容。要让规则真正更改工作表,我缺少什么 For Each ws In ThisWorkbook.Worksheets If ws.Name Like "*Management*" Then Exit Sub lastRow
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "*Management*" Then Exit Sub
lastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
lastCol = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1
ws.Cells.FormatConditions.Delete
For col = 1 To lastCol
Set rng = ws.Range(ws.Cells(2, col), ws.Cells(lastRow, col))
formulaStr = "=BITWISE_AND(2^INDEX(" & wsSchema.Name & "!$" & schemaLastCol & "$2" & _
":$" & schemaLastCol & "$" & schemaLastRow & ", MATCH(1, (" & wsSchema.Name & "!$A$2:$A$" & _
schemaLastRow & "=""" & ws.Name & """)*(" & wsSchema.Name & "!$B$2:$B$" & schemaLastRow & "=$" & _
XLCol(col) & "$1),0)), INDEX($" & XLCol(lastCol) & "$1:$" & XLCol(lastCol) & "$" & lastRow & ",ROW()))"
With rng
.FormatConditions.Add xlExpression, , formulaStr & " = 0"
With .FormatConditions(1)
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = RGB(255, 0, 0)
.Interior.TintAndShade = 0
.StopIfTrue = False
.SetFirstPriority
End With
.FormatConditions.Add xlExpression, , formulaStr & " > 0"
With .FormatConditions(2)
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = RGB(0, 255, 0)
.Interior.TintAndShade = 0
.StopIfTrue = False
.SetFirstPriority
End With
End With
Next
下一步尝试此工作簿。刷新全部或应用程序。计算尝试录制一个宏,查看在执行编辑规则操作时调用了哪些方法。我这样做了,调用的方法都包含在两个内部with块中,但没有JOYES。是的,尝试了两种方法,但均未成功。有趣的是,我发现当打开工作簿时,格式已经准备好了。不幸的是,这无助于解决问题,因为工作簿关闭后,所有数据都需要从工作簿中清除。数据由另一个例程从SQL Server数据库中提取