使用FindNext的UDF似乎在没有警告的情况下中止

时间:2015-02-25 09:15:34

标签: excel excel-vba excel-udf vba

我有以下用户定义的函数,它试图在一个范围内找到第n个非空单元格:

Option Explicit

Function finn_prioritert_oppgave(nummer As Long) As String

  Dim i As Long, r As Range, c As Range

  Set r = Range(PDCA.Range("L9"), PDCA.Range("L1048576").End(xlUp))
  i = 1

  Set c = r.Find(What:="*", LookIn:=xlValues, LookAt:=xlWhole)

  While (Not c Is Nothing) And (Intersect(c, PDCA.Rows(9)) Is Nothing) And (i < nummer)

    Debug.Print c
    Set c = r.FindNext(c)
    Debug.Print c
    Debug.Print CBool(c Is Nothing)
    i = i + 1

  Wend

  If c Is Nothing Then
    finn_prioritert_oppgave = "#N/A"
  Else
    finn_prioritert_oppgave = c.Offset(0, -10).Value
  End If

End Function

使用1运行它作为参数工作正常,大概是因为它没有进入While - 循环并点击FindNext,但运行它时任意大的值因为参数导致单元格它被调用来显示#VALUE! - 警报。

查看我的即时窗口中显示的内容也很奇怪,因为执行Debug.Print后的两条FindNext消息无法打印,但我没有收到警报。

我在立即窗口中获得的输出,以及以2作为参数调用的UDF只是一个x:

enter image description here
调用函数的区域看起来像this(第一行是调用的UDF,参数为1,第二行是调用2作为参数),而包含数据的区域看起来像{{3 }}

所以我想知道的是,为什么FindNext无法在范围内找到第二个非空单元格,为什么函数在没有任何警告的情况下中止?

1 个答案:

答案 0 :(得分:2)

FindNext在UDF中不起作用。您只需重复原始的Find操作。

相关问题