-1

我有一个函数 FILTER_COL 应该过滤范围 rng 中的单元格,这些单元格与 from_cell 的单元格颜色相同:

Function FILTER_COL(from_cell As String, rng As Range)
Dim colour As Integer
For Each cell In Range(from_cell)
    colour = cell.Interior.ColorIndex
Next cell

Dim Out As Variant
For Each cell In rng
    If cell.Interior.ColorIndex = colour Then
        Out.Add (cell.Value)
    End If
Next cell
FILTER_COL = Out
End Function

然后我将结果放入 SUM 中,如下所示:

SUM(FILTER_COL("A55",K50:K52))

它给出了一个#VALUE!如果 K50:K52 范围内有任何与 A55 颜色相同的单元格,则会出错。

4

1 回答 1

0

您必须返回一个数组或一个范围对象。范围对象会更适合,但如果没有颜色匹配,afaik 无法指定空范围。所以我使用了一个双打数组:

Function FILTER_COL(from_cell As String, rng As Range) As Double()
    Dim v() As Double
    ReDim v(rng.Count)
    Dim i As Integer
    i = 0

    Dim colour As Integer
    colour = Range(from_cell).Interior.ColorIndex

    Dim cell As Variant
    For Each cell In rng
        If cell.Interior.ColorIndex = colour And IsNumeric(cell.Value) Then
            v(i) = cell.Value
            i = i + 1
        End If
    Next cell

    FILTER_COL = v
End Function
于 2013-07-04T20:25:57.203 回答