Vba 用户定义的工作表函数返回#值!错误,而不是单元格颜色索引
请有人帮助我了解此功能的问题:Vba 用户定义的工作表函数返回#值!错误,而不是单元格颜色索引,vba,excel,cell,Vba,Excel,Cell,请有人帮助我了解此功能的问题: '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)
'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
测试按预期工作,但当我在工作表单元格中使用函数时:
=cellDisplayCol(A1)
它返回#值!错误
你知道为什么吗?我该修什么
非常感谢 检查msdn后,您似乎无法在UDF中使用
.DisplayFormat
:
请注意,DisplayFormat
属性在用户定义的函数中不起作用。例如,在返回单元格内部颜色的工作表函数中,使用类似于的行:范围(n).DisplayFormat.Interior.ColorIndex
。当工作表函数执行时,它返回一个#值代码>错误
感谢Gareth的研究。我的问题是我需要单元格的显示颜色,因为(建议的)ColorIndex属性不受条件格式的影响。实际显示的颜色对我来说至关重要,如果不将实际(非常复杂)的条件格式逻辑翻译/复制到函数中,我就找不到另一种方法来获得它。有什么建议吗?方向?谢谢@MaxSegal为了避免代码重复,将颜色选择逻辑放入VBA函数中,并从单元格和条件格式规则中调用它。