Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my take, even if Sid already has a great answer. I recreated a table with name <code>test</code> and positioned it at <code>A1</code>. I used a minor edit of your code and it works fine for me.</p> <pre><code>Sub Test() Dim v As Range Set v = Range("test") v.ClearFormats v.FormatConditions.Add Type:=xlExpression, Formula1:="=LEN(TRIM(A1))=0" With v.FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.399945066682943 End With v.FormatConditions(1).StopIfTrue = False End Sub </code></pre> <p>Just as a note, though, the usage of <code>A2</code> inside the formula can produce inflexible results, especially compared to what Sid used in his code above.</p> <p>Hope it helps (or at least gives some insight)!</p> <p><strong>SECOND TAKE:</strong></p> <p>This has been bothering me since the other day so I'll give it another shot. Apparently, based on this <a href="http://support.microsoft.com/kb/895562" rel="nofollow noreferrer">Microsoft Support nugget</a>, there seems to be issues with CF as it is. Two workarounds exist: either by using absolute reference or by selecting the cell first before applying CF.</p> <p>I played around a bit and got wrong results a lot of times with absolute reference. However, one simple approach works. We select the first cell of <code>Table1</code> and give it CF, and then we use the simplest approach in the book: format painter! We also replaced <code>.ClearFormats</code> with <code>.FormatConditions.Delete</code>.</p> <p>Here's a variation of your code with the aforementioned approach:</p> <pre><code>Sub Test() Dim Table1 As Range: Set Table1 = ThisWorkbook.Sheets("Sheet1").Range("Table1") Start = Timer() Application.ScreenUpdating = False Table1.FormatConditions.Delete With Table1.Cells(2, 1) 'With Range("B7") .Select .FormatConditions.Add Type:=xlExpression, Formula1:= _ "=LEN(TRIM(B7))=0" .FormatConditions(.FormatConditions.Count).SetFirstPriority With .FormatConditions(1).Interior .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent2 .TintAndShade = 0.399945066682943 End With .FormatConditions(1).StopIfTrue = False .Copy End With Table1.PasteSpecial xlPasteFormats 'or the next one 'Range("B7:AO99").PasteSpecial xlPasteFormats Application.CutCopyMode = False Application.ScreenUpdating = True Debug.Print Timer() - Start End Sub </code></pre> <p>Here's a preview of results.</p> <p><img src="https://i.stack.imgur.com/8LtUd.png" alt="Sample Table1"></p> <p>Execution times (in seconds) are:</p> <ul> <li>4.296875E-02</li> <li>4.492188E-02</li> <li>5.273438E-02</li> <li>5.859375E-02</li> <li>0.0625</li> </ul> <p>These are much faster than a previous attempt of mine where I looped through all the cells and added CF to each.</p> <p>Hope this helps you!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload