vba 识别红色单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16330345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identify cells in red
提问by user2341632
The title may be typically simple, but I did not get a solution yet even after researching over several forums on the net.
标题可能通常很简单,但即使在网上搜索了几个论坛后,我也没有得到解决方案。
Let me explain the problem.
让我解释一下这个问题。
I have an excel workbook where few columns uses if conditions that refers other cells. Based on the cell's content, the back color of the cell is defined. For eg. If a cell is blank, it automatically changes to red. These red cells indicates missing information. So I am trying to design a macro to identify these red cells and get the address of each red cell.
我有一个 excel 工作簿,其中很少有列使用 if 条件引用其他单元格。根据单元格的内容,定义单元格的背景颜色。例如。如果单元格为空白,它会自动变为红色。这些红色单元格表示缺少信息。所以我试图设计一个宏来识别这些红细胞并获取每个红细胞的地址。
For this I used the code for testing, MsgBox IIf(ActiveCell.Interior.ColorIndex = 3, "Yes", "No")
为此,我使用了代码进行测试, MsgBox IIf(ActiveCell.Interior.ColorIndex = 3, "Yes", "No")
But this does not work. The reason is, the macro identifies the color index as -4142 irrespective of what color it is. Whether it is white or blue or red, it still shows -4142.
但这不起作用。原因是,无论它是什么颜色,宏都将颜色索引标识为 -4142。无论是白色还是蓝色或红色,它仍然显示-4142。
When I asked this question in a forum, I was redirected to this page.
当我在论坛上问这个问题时,我被重定向到这个页面。
http://www.cpearson.com/excel/CFColors.htm
http://www.cpearson.com/excel/CFColors.htm
I tested the functions given in that page. It works fine for the sample workbooks I created. But not in the workbook that I am trying to validate.
我测试了该页面中给出的功能。它适用于我创建的示例工作簿。但不在我试图验证的工作簿中。
Could you please help me?
请你帮助我好吗?
回答by Alistair Weir
Using the following works for me where I made a few cells in C default red.
使用以下对我有用,我在 C 默认红色中制作了几个单元格。
Dim ws As Worksheet
Set ws = Sheets("sheet1")
Dim i As Integer
i = 1
Do Until i = 11
If ws.Range("C" & i).Interior.Color = RGB(255, 0, 0) Then
debug.print "C" & i & " is red!!"
End If
i = i + 1
Loop
Edit:
编辑:
This also works using colorindex = 3
I think your problem is a combination of activecell
and IIf
这也适用于colorindex = 3
我认为您的问题是activecell
和IIf