简体   繁体   中英

Conditional formatting (Change Cell Color) if cell value <> “”

so to give some context, I have an application running where I have written code where basically, on a button press:-

  • A new sheet is created
  • The columns "A:L" in the new sheet are conditionally formatted
  • The condition is If any cell in column A has a value then that cell color is red . Similarly, a different color condition for every column

To achieve this, I wrote the following code snippet in my Sub:

Dim condA As FormatCondition
Set condA = Range("A5:A500").FormatConditions.Add(Type:=xlCellValue, Operator:=xlNotEqual, Formula1:="")

Now the problem is that the execution stops at the Set statement with the error:

Invalid procedure call or argument

What is the problem with the statements and how can you rewrite the code to change the interior color of any cell in the column if a value is entered or present in the cell? Thanks!

If you wish to test for cells that are blank, use a formula of ="" . To put that formula in as the Formula1 parameter, you would use Formula1:="="""""


Note: This won't strictly test for any value being entered by the user, because the user could enter a single ' into a cell, or they could insert a formula of ="" into a cell, and both those cells will not be coloured using your conditional format. That probably isn't likely to be an issue but, if it is, a Formula1 of "=AND(A5="""",A5=0)" (ie testing that the cell is both a blank string and a number zero - which should only be True if the cell is empty) might work. (Untested)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM