0

我有以下 VBA 功能:

Function IndexOfColor(InRange As Range, ColorIndex As Long) As Excel.Range
Dim R As Range

Application.Volatile True
If IsValidColorIndex(ColorIndex) = False Then
    IndexOfColor = 0
    Exit Function
End If

For Each R In InRange.Cells
    If R.Interior.ColorIndex = ColorIndex Then
        IndexOfColor = R
        Exit Function
    End If
Next R

IndexOfColor = 0
End Function

在我的excel表中,我称之为:

=IndexOfColor(D15:M24,37)

并且总是得到"#VALUE"。我已经调试了这个函数,直到最后,没有问题。虽然这应该只返回 1 个结果(我正在查看范围,并且只有一个彩色单元格),但我也尝试将其设为数组结果(CTRL-SHIFT-ENTER)。

建议?

4

1 回答 1

1

这是你正在尝试的吗?如果单元格没有小数,则可以替换VariantLong

Function IndexOfColor(InRange As Range, ColorIndex As Long) As Variant
    Dim R As Range

    IndexOfColor = 0

    On Error GoTo Whoa

    Application.Volatile True

    If Not IsValidColorIndex(ColorIndex) = False Then
        For Each R In InRange.Cells
            If R.Interior.ColorIndex = ColorIndex Then
                IndexOfColor = R.Value
                Exit For
            End If
        Next R
    End If
Whoa:
End Function
于 2012-08-16T13:25:49.387 回答