选择列 A、F 和 M,并将 F1 作为活动单元格。使用以下公式创建新的 CFR。
=or(iferror(left(e1)="P", false), left(g1)="P")
在 VBA 中创建它的问题在于公式; A 列左侧没有列,并且三列的任何联合将始终将 A1 视为“活动单元格”,无论联合是如何创建的。 .Range("G:G, M:M, A:A,") 与 .Range("A:A, G:G, M:M"); A1 是“活动单元格”。一种解决方案是临时切换到 xlR1C1,其中 RC[-1] 可用于引用 A 列左侧不存在的列。
Option Explicit
Sub meh()
Dim refStyle As Long, xlR1C1formula As String
'store original reference style
refStyle = Application.ReferenceStyle
'make it xlR1C1 reference style
Application.ReferenceStyle = xlR1C1
With Worksheets("sheet1")
With .Range("A:A, G:G, M:M")
.FormatConditions.Delete
xlR1C1formula = "=or(iferror(left(rc[-1])=char(80), false), left(rc[1])=char(80))"
With .FormatConditions.Add(Type:=xlExpression, Formula1:=xlR1C1formula)
.Interior.ColorIndex = 5
.NumberFormat = ";;;"
End With
xlR1C1formula = "=or(iferror(left(rc[-1])=char(84), false), left(rc[1])=char(84))"
With .FormatConditions.Add(Type:=xlExpression, Formula1:=xlR1C1formula)
.Interior.ColorIndex = 22
.NumberFormat = ";;;"
End With
End With
'switch back
Application.ReferenceStyle = refStyle
End With
End Sub