我有一个工作表,其中包含两个UDF。它们的内容(除了一行)对手头的问题并不那么感兴趣,但对于好奇的人来说,它们在这里:
Function finn_prioritert_oppgave(nummer As Long) As String
Dim i As Long, r As Range, c As Range
Set r = Range(PDCA.Range("N11"), PDCA.Range("N1048576").End(xlUp))
If Not Intersect(r, PDCA.Range("N10")) Is Nothing Then
Set r = PDCA.Range("N11")
End If
For Each c In r
If Not IsEmpty(c) Then
nummer = nummer - 1
End If
If nummer = 0 Then
Exit For
End If
Next
If nummer > 0 Then
finn_prioritert_oppgave = CVErr(xlErrNA)
Else
finn_prioritert_oppgave = c.Offset(0, -11).Value
End If
End Function
Function finn_status_oppgave(oppgave As Range) As String
Application.Volatile
Dim r As Range, i As Long, satisfied As Boolean
Call deaktiver
Set r = Range(PDCA.Range("C11"), PDCA.Range("C1048576").End(xlUp))
Set r = r.Find(what:=oppgave, LookIn:=xlValues, lookat:=xlWhole)
finn_status_oppgave = ""
If Not r Is Nothing Then
Set r = PDCA.Range("J" & CStr(r.Row) & ":M" & CStr(r.Row))
i = 4
satisfied = False
Do
If Not IsEmpty(r.Cells(1, i)) Then
Debug.Print PDCA.Range("J9:M9").Cells(1, i)
finn_status_oppgave = PDCA.Range("J9:M9").Cells(1, i)
satisfied = True
End If
i = i - 1
Loop While i >= 1 And Not satisfied
End If
Call reaktiver
End Function据我所知,第二个函数的问题是,因为它包含行Application.Volatile。整个上午我都在与工作簿做斗争,当我尝试重命名工作表时,我无法确定Excel崩溃的原因。最后,在搜索了我的代码并查找错误之后,我终于在一个Excel论坛上遇到了这条线,该论坛声称问题的发生是因为上面提到的函数。
显然
如果您有一个易失性函数(您的代码函数包含"Application.Volatile (True)“标记),其中您更改了DisplayAlerts状态(例如,"Application.DisplayAlerts = False"),那么如果您在包含这两个命令的工作簿中更改工作表的名称,Excel (直到2010年,还没有经过2013年的测试)将崩溃。
我没有在代码中使用Application.DisplayAlerts = False,但症状非常相似,因此我确信是易失性函数导致了这些问题:

我能做些什么来防止这一错误的发生吗?还是应该让它非易失性,并使用例如Application.CalculateFull在Workbook_SheetChange-event上进行计算?
发布于 2015-03-23 11:51:17
您的deaktiver和reaktiver几乎肯定是Subs,因为您不向他们解析任何东西。没有很多不需要争论的函数!
如果deaktiver或reaktiver试图更改任何应用程序、工作簿、工作表或范围属性,则调用函数将失败!例如,这将包括设置Application.DisplayAlerts = False等内容。
工作表中的函数只能更改它们所在单元格的值。它们只能返回函数结果。
你用函数来计算-你的子代来改变事物。
子函数可以调用函数,但是函数不应该调用子函数。
https://stackoverflow.com/questions/29207513
复制相似问题