Excel 如何检查特定范围内是否有彩色单元格?
我正在寻找一个Excel公式,用于检查一行中是否有彩色单元格。 如果有,在第2列的同一行中,实际上是B列,则打印是,否则打印否 我在第二行尝试了这个公式:Excel 如何检查特定范围内是否有彩色单元格?,excel,vba,excel-formula,Excel,Vba,Excel Formula,我正在寻找一个Excel公式,用于检查一行中是否有彩色单元格。 如果有,在第2列的同一行中,实际上是B列,则打印是,否则打印否 我在第二行尝试了这个公式: =IF(2:2.Interior.Color=4,"Yes","No") 也许解决方案应该用VBA代码而不是Excel公式编写 以下是我希望它的外观: 在vba中,您可以尝试使用以下代码:在本例中,我使用了C2到F10的范围,但您可以更改进入startRow、endRow、startCol end I
=IF(2:2.Interior.Color=4,"Yes","No")
也许解决方案应该用VBA代码而不是Excel公式编写
以下是我希望它的外观:
在vba中,您可以尝试使用以下代码:在本例中,我使用了C2到F10的范围,但您可以更改进入startRow、endRow、startCol end In endCol的值以更改范围
Sub MyInteriorColor()
Const COLOR As Integer = 4 ' here you can change the color
Dim startRow, endRow, startCol, endCol, i, c As Integer
startRow = 2 ' Read the interior color from second row
endRow = 10 ' Last row where i read the interior color
startCol = 3 ' Column where i start to read the interior color
endCol = 6 ' Column where i finish to read interior color
For i = startRow To endRow
For c = startCol To endCol
If Cells(i, c).Interior.ColorIndex = COLOR Then
Cells(i, 2) = "YES"
Exit For
Else
Cells(i, 2) = "NO"
End If
Next c
Next i
End Sub
在vba中,您可以尝试使用以下代码:在本例中,我使用了C2到F10的范围,但您可以更改进入startRow、endRow、startCol end In endCol的值以更改范围
Sub MyInteriorColor()
Const COLOR As Integer = 4 ' here you can change the color
Dim startRow, endRow, startCol, endCol, i, c As Integer
startRow = 2 ' Read the interior color from second row
endRow = 10 ' Last row where i read the interior color
startCol = 3 ' Column where i start to read the interior color
endCol = 6 ' Column where i finish to read interior color
For i = startRow To endRow
For c = startCol To endCol
If Cells(i, c).Interior.ColorIndex = COLOR Then
Cells(i, 2) = "YES"
Exit For
Else
Cells(i, 2) = "NO"
End If
Next c
Next i
End Sub
我认为UDF可能是更好的方法。 这样每个细胞都可以指向它需要的任何范围。 将此代码放在模块中:
Function IsGreen(rng as range)
IsGreen = "No"
for each cell in rng
If cell.Interior.ColorIndex = 4 then
IsGreen = "Yes"
Exit for
end if
next cell
End function
要使用它,请在第2列中输入公式:
=IsGreen(C2:G2)
它应该返回yes/no。
我在手机上键入了这个答案,所以我无法测试它,但我相信它会起作用。我认为UDF可能是更好的方法。 这样每个细胞都可以指向它需要的任何范围。 将此代码放在模块中:
Function IsGreen(rng as range)
IsGreen = "No"
for each cell in rng
If cell.Interior.ColorIndex = 4 then
IsGreen = "Yes"
Exit for
end if
next cell
End function
要使用它,请在第2列中输入公式:
=IsGreen(C2:G2)
它应该返回yes/no。
我在手机上键入了这个答案,因此我无法测试它,但我相信它会起作用。检查不起作用-发生了什么而不是它起作用?检查不起作用-发生了什么而不是它起作用?非常感谢。工作完美。有没有办法知道有多少已使用的行/包含数据的行?所以我可以使变量endRow为常量,而不是硬编码。谢谢是的,你可以使用这个:lastRow=.Cells.Rows.Count,A.EndxlUp.Row lastColumn=Cells1,Columns.Count.EndxlToLeft.Column如果我在帖子中的回答对你有帮助,我感谢你的支持。非常感谢你。工作完美。有没有办法知道有多少已使用的行/包含数据的行?所以我可以使变量endRow为常量,而不是硬编码。谢谢是的,你可以使用这个:lastRow=.Cells.Rows.Count,A.EndxlUp.Row lastColumn=Cells1,Columns.Count.EndxlToLeft.Column如果我在帖子中的回答对你有帮助,我感谢你的支持。谢谢,我喜欢你的解决方案@Andreas!有一个问题,如果我有一个包含许多单元格的工作表来插入函数,文件会变慢?我不确定,但我假设是这样,因为每个单元格都会重新计算。另一个解决方案执行所有操作一次,然后什么也不做,直到您希望通过运行代码手动更新为止。编辑:我以为我是在和OP说话。是的,我相信如果有很多事情发生变化,你的代码会比我的好。但是你的需要手动运行。我喜欢你的解决方案@Andreas!有一个问题,如果我有一个包含许多单元格的工作表来插入函数,文件会变慢?我不确定,但我假设是这样,因为每个单元格都会重新计算。另一个解决方案执行所有操作一次,然后什么也不做,直到您希望通过运行代码手动更新为止。编辑:我以为我是在和OP说话。是的,我相信如果有很多事情发生变化,你的代码会比我的好。但您的需要手动运行。