2012-08-16 211 views
0

我有以下VBA函数:Excel的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)。

对此提出建议?

+0

什么是CI取代Variant?它不应该是'ColorIndex'吗? – 2012-08-16 13:05:47

+0

'虽然这应该只返回1结果'也是'1'你是如何到达的?该函数应该返回一个范围')作为Excel.Range'?也许如果你能够准确地解释你想做什么? – 2012-08-16 13:07:49

+0

我看到你的函数定义了两个参数'InRange'和'ColorIndex',但我看到你用3个参数调用函数'D15:M24','37'和'0' – SeanC 2012-08-16 13:10:01

回答

1

这是你正在尝试?如果细胞不会有小数,那么你可以用Long

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 
+0

“As Long”就是我一直在寻找的东西。实际上,我之前有过这个,但被格式错误所蒙蔽,所以认为这个数字是某种指针。 – Jahmic 2012-08-17 11:01:24

相关问题