[英]Excel UDF - Gives #Value error when referencing another workbook sheet
[英]Excel VBA UDF Returns #VALUE Error When Adding Or Deleting Unrelated Sheet
第一次发帖,如有错误请见谅!
所以,我的 UDF 有一个很奇怪的问题。 在我的工作簿中,我有一个名为“标准阶段表”的不可见“模板”表,以及一个用户可以激活的子例程,该子例程将该模板表复制到用户可以使用的新的可见表中。 整个工作簿中将有该模板表的许多副本,但它们都有唯一的名称。
我的 UDF 位于该模板表的多个位置,因此位于用户制作的模板表的每个副本上。 在其中一张工作表中工作时,UDF 工作得很好,并返回我期望的值。
但是,当用户添加模板表的新副本时,有时 UDF 会失控并在使用 UDF 的每个地方返回 #VALUE 错误。
此外,当用户删除模板表的一个副本时,UDF 总是会出问题并在使用 UDF 的每个地方返回 #VALUE 错误。
我没有使用 ActiveSheet 或类似的东西,我相信我正确地给出了我在 UDF 中使用的范围的完整引用。 任何帮助将不胜感激,我在这里受困! UDF 的代码如下。
另外,因为我确定我会被问到这个问题,所以我的代码中的 neColumn 变量是我在几个子例程和 UDF 中使用的公共变量。 它在我的模块的开头定义。 此外,我也在模块的开头使用了 Option Explicit。
谢谢!
Public Function fSum(ByVal Target As Range, bExtended As Boolean) As Single
'This function returns a sum, based on a range provided in the cell that holds the function.
'It checks to see if that line item has been marked as Non-Extended, based on the NE column
'that can be check marked. If that line item is marked NE, then only the NE sum columns can
'use that line item as part of their sum, and those values are removed from the E columns.
Dim sSum As Single
Dim i As Integer
Dim n As Integer
'This small section is used to determine complete references to the cell calling the function.
Dim sheetName As String
sheetName = Application.Caller.Parent.Name
'Loop through provided range, and sum up the contents based on whether they have been marked NE or not.
i = 1
n = Target.row
sSum = 0
If Sheets(sheetName).Visible = True Then
While i < Target.Rows.Count
If (bExtended = True) Then
If Sheets(sheetName).Range(neColumn.Address).Cells(n, 1) = vbNullString Then
sSum = sSum + Sheets(sheetName).Range(Target.Address).Cells(i, 1).Value
End If
Else
If Sheets(sheetName).Range(neColumn.Address).Cells(n, 1) <> vbNullString Then
sSum = sSum + Sheets(sheetName).Range(Target.Address).Cells(i, 1).Value
End If
End If
i = i + 1
n = n + 1
Wend
End If
fSum = sSum
End Function
在子孙的答案中总结评论线程:
Long
而不是Integer
,更喜欢Do While...Loop
到While...Wend
,删除.Visible
检查...SUMIFS
的功能,因此您可能会考虑走这条路。原因是你的neColumn
变量变成了Nothing
,因为 Excel 是 Volatile。
我假设你的模块的开头看起来像这样:
Option Explicit
Public neColumn As Range
Sub Auto_Open()
Set neColumn = Sheet1.Range("A1:B2")
End Sub
当您打开工作簿时,您调用Auto_Open
Sub 来Set
neColumn
变量。 但是 - 当某些操作发生时,Excel 会重建 VBA,这会将公共变量(例如neColumn
)重置为其默认值(对于诸如Range
类的Object
,它是Nothing
)。 触发它的一种简单方法是故意抛出错误,例如尝试运行:
Sub ThrowErr()
NotDefined = 1
End Sub
您可以通过在fSum
代码中添加以下行来使其更明显:
If neColumn Is Nothing Then Stop
当neColumn
被重置为Nothing
,您要么需要一种方法来恢复它,要么找到一种非易失性的方法来存储它。
我假设这不适合成为Const
,因为否则它已经是,但您可以将其转换为命名范围,或将Address
存储在隐藏的工作表/ CustomDocumentProperty
。 这些选项还允许您在重新打开工作簿时保存工作簿时存储neColumn
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.