简体   繁体   English

如果单元格值<>“”,则为条件格式(更改单元格颜色)

[英]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 新工作表中的“ A:L”列是有条件的格式
  • The condition is If any cell in column A has a value then that cell color is red . 条件为: 如果A列中的任何单元格都具有值,则该单元格颜色为红色 Similarly, a different color condition for every column 同样,每列的颜色条件不同

To achieve this, I wrote the following code snippet in my Sub: 为此,我在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: 现在的问题是执行在Set statement处停止并出现错误:

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:="=""""" 要将公式作为Formula1参数放入,可以使用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. 这可能不太可能成为问题,但如果是,则Formula1"=AND(A5="""",A5=0)" (即测试单元格既是空白字符串是数字零) -这应该只是True如果单元格是空的) 可能会奏效。 (Untested) (未经测试)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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