#用户定义函数VBA中的值错误

#用户定义函数VBA中的值错误,vba,excel,excel-formula,excel-2010,Vba,Excel,Excel Formula,Excel 2010,我正在编写一个函数来查找当前活动单元格上方具有不同内部颜色(带条件格式)的第一行。当我使用debug.print或msgbox打印时,该函数工作,但当我在工作表中直接调用它时,该函数不工作。第一行是标题,这就是为什么我排除了第一行 Public Function blockStart() As Long Dim currColor As Long currColor = activecell.DisplayFormat.Interior.ColorIndex Dim c

我正在编写一个函数来查找当前活动单元格上方具有不同内部颜色(带条件格式)的第一行。当我使用
debug.print
msgbox
打印时,该函数工作,但当我在工作表中直接调用它时,该函数不工作。第一行是标题,这就是为什么我排除了第一行

Public Function blockStart() As Long
    Dim currColor As Long
    currColor = activecell.DisplayFormat.Interior.ColorIndex

    Dim currCol As String
    currCol = Number2Letter(activecell.Column)

    Dim startRow As Long
    startRow = activecell.Row

    'Find Ceiling
    Do While startRow >= 2
        If Range(currCol & startRow).DisplayFormat.Interior.ColorIndex <> currColor Then
            startRow = startRow + 1
            Exit Do
        End If
        startRow = startRow - 1
    Loop

    If startRow = 1 Then startRow = 2   ' at the very top

    blockStart = startRow

End Function
公共函数blockStart()尽可能长
暗咖喱色和长咖喱色一样
currColor=activecell.DisplayFormat.Interior.ColorIndex
暗咖喱线
currCol=number2字母(activecell.Column)
黯淡的星空如长
startRow=activecell.Row
“找到天花板
开始时执行此操作>=2
如果范围(currCol&startRow).DisplayFormat.Interior.ColorIndex currColor然后
startRow=startRow+1
退出Do
如果结束
startRow=startRow-1
环
如果startRow=1,则startRow=2'位于最顶部
blockStart=startRow
端函数

当我在工作表中直接调用函数时,会提示
#value
。请让我知道我做错了什么,谢谢

为了不让您觉得这是不可能的,一种可能的方法是将BlockStart函数的整个主体放入工作表的SelectionChange事件中。然后,您可以将startRow的值分配给您尝试使用blockStart函数的单元格,而不是写有
blockStart=startRow
的行


如果多个单元格使用BlockStart函数,请将该值分配给某个未使用的单元格,并让其他单元格引用该值。

不幸的是,
DisplayFormat
不适用于UDF……真的吗?我认为不起作用的部分实际上是改变一些东西,但从中阅读也不起作用?我不确定我是否理解你的问题,但在任何情况下,在工作表中调用UDF都不能与
DisplayFormat
一起使用。做了一些研究,事实证明是这样的。。。谢谢谢谢你的修复!我不确定这是否对我有效,因为我将在30000多个细胞中加入。。。