我有以下用户定义的函数,它试图在一个范围内找到第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-loop并命中FindNext,但是运行它时会有更大的值,因为参数会导致调用它的单元格显示#VALUE!-alert。
查看即时窗口中显示的内容也很奇怪,因为执行Debug.Print后的两条FindNext消息不会打印,尽管我没有收到警告。
我在“立即”窗口中获得的输出,其参数为2调用的UDF只是一个x:

函数的调用区域看起来类似于这 (第一行是以1作为参数调用的UDF,第二行是以2作为参数调用的),而包含数据的区域看起来像这。
所以,我想知道的是,为什么FindNext在范围内找不到第二个非空单元,为什么函数在没有任何警告的情况下就中止了?
发布于 2015-02-25 11:04:57
FindNext在UDF中不工作。您必须简单地重复最初的Find操作。
https://stackoverflow.com/questions/28715135
复制相似问题