Vba 基于颜色的返回范围
我试图在excel中编写一个UDF,它在给定范围内迭代,查找该范围内给定背景颜色的单元格,然后返回包含该背景颜色的单元格范围。我对VBA非常陌生,我甚至不确定我是否能通过谷歌搜索找到我想要的东西。这是我到目前为止给出的代码Vba 基于颜色的返回范围,vba,excel,Vba,Excel,我试图在excel中编写一个UDF,它在给定范围内迭代,查找该范围内给定背景颜色的单元格,然后返回包含该背景颜色的单元格范围。我对VBA非常陌生,我甚至不确定我是否能通过谷歌搜索找到我想要的东西。这是我到目前为止给出的代码 Function findRed(MyRange As Range) As Variant Dim redRange As Range Application.Volatile For Each cell In MyRange If cell.Interior.Col
Function findRed(MyRange As Range) As Variant
Dim redRange As Range
Application.Volatile
For Each cell In MyRange
If cell.Interior.ColorIndex = 3 Then
End If
Next cell
End Function
我在web上发现了一个简单的函数,可以返回包含背景颜色的单元格计数,这很好,但我不知道在IF语句中该做什么。找到红色的细胞然后做什么?以返回实际为红色的单元格区域。取决于您需要对找到的区域执行什么操作?例如: MsgBox cell.Address将返回单元格的地址 MsgBox cell.value返回已测试的单元格内容:
Function findRed(MyRange As Range) As Range
Dim redRange As Range, cell As Range
Application.Volatile
For Each cell In MyRange
If cell.Interior.ColorIndex = 3 Then
If redRange Is Nothing Then
Set redRange = cell
Else
Set redRange = Application.Union(redRange, cell)
End If
End If
Next cell
Set findRed = redRange
End Function
编辑:如果要检查返回的范围地址
Function findRedAddress(MyRange As Range) As String
Application.Volatile
On Error Resume Next
findRedAddress = findRed(MyRange).Address(False, False)
End Function
我计划在以后的计算中使用返回的范围。本质上是工作簿中的另一个IF语句。比如,如果redRange是A1:A10,然后乘以10。你可以检查Tim的答案,看看是否将范围放在一起。这对我来说很有意义。我在工作簿上使用=findRedA1:A99作为测试,它返回值。是否有一种方法可以以可读的格式输出它,以双重检查它是否指示正确的单元格?我刚刚使用了MsgBox,它显示了范围。非常感谢!