2017-04-03 50 views
0

像任何人使用條件格式,但也做了大量的複製和粘貼,我真的很厭惡我的表單上的許多規則,因爲我複製粘貼了一些單元格。所以我的想法是我應該硬編碼條件格式,然後製作一個宏來清除所有的格式並重新應用它。唉VBA今天似乎恨我!VBA條件編程微妙

爲什麼不這項工作:

Sheets("Log").Cells.FormatConditions.Delete 
With Range("D1:H63") 
    .FormatConditions.Add Type:=xlExpression, Formula1:= _ 
     "=D1='Working')" 
    With .FormatConditions(.FormatConditions.Count) 
     .SetFirstPriority 
     With .Interior 
      .PatternColorIndex = xlAutomatic 
      .Color = 5287936 
      .TintAndShade = 0 
     End With 
    End With 
End With 

在這個例子中,我從其他地方得到的也是如此:

With Range("B3:H63") 
     .FormatConditions.Add Type:=xlExpression, Formula1:= _ 
      "=IF($D3="""",FALSE,IF($F3>=$E3,TRUE,FALSE))" 
     With .FormatConditions(.FormatConditions.Count) 
      .SetFirstPriority 
      With .Interior 
       .PatternColorIndex = xlAutomatic 
       .Color = 5287936 
       .TintAndShade = 0 
      End With 
     End With 
    End With 

回答

2

我看到兩個問題是代碼:

首先,在

.FormatConditions.Add Type:=xlExpression, Formula1:= _ 
     "=D1='Working')" 

你有一個流浪)應該被淘汰

其次,'Working'沒有公式中的一個有效的文字,但你可以引用它

像這樣:

.FormatConditions.Add Type:=xlExpression, Formula1:= _ 
     "=D1=""'Working'""" 

,在意義上是工作它在單元格中插入條件格式而不會引發運行時錯誤。無論是否插入您希望觸發的格式,都是一個單獨的問題,根據您提供的信息我無法回答。