选择列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
顺便说一句,如果你想在单元格文本/值“消失“,那么不要设置字体颜色;设置';;;'的自定义数字格式。 – Jeeped
谢谢你为这两个伟大的解决方案。不过,我用vba实现这个有点麻烦。当我在gif中手动跟踪你的解决方案时,它完全按照预期工作,但是当我尝试使用宏时,列M没有得到格式化。这里是[code](http://imgur.com/a/loQWj)我正在使用,这里是[结果](http://imgur.com/a/8CEwo)。任何帮助总是表示感谢你! – jsweeney
请参阅上面的我的VBA解决方案。我使用char(80)和char(84)来确定P和T,因为我讨厌在引用的字符串中加倍双引号。 – Jeeped