用户定义的工作表函数返回#VALUE! 错误而不是单元格颜色索引

有人可以帮助我了解这个function的问题:

'the function returns the DISPLAYED color of specified cell Public Function cellDisplayCol(ByRef c As Range) As Long cellDisplayCol = c.DisplayFormat.Interior.ColorIndex End Function Sub test() MsgBox cellDisplayCol(ActiveCell) End Sub 

该testing按预期工作,但是当我在工作表单元格中使用该函数时:= cellDisplayCol(A1)

它返回#VALUE! 错误…

任何想法为什么? 我该怎么修复?

非常感谢!

检查完msdn后,看起来你不能在UDF中使用.DisplayFormat

请注意, DisplayFormat属性在用户定义的函数中不起作用。 例如,在返回单元格的内部颜色的工作表函数中,可以使用类似于以下的直线:
Range(n).DisplayFormat.Interior.ColorIndex 。 当工作表函数执行时,它会返回一个#VALUE! 错误。

http://msdn.microsoft.com/en-us/library/office/ff838814(v=office.15).aspx