[英]Answer in Excel cell is not value while debugging
I have a custom formula that I am trying to use in my Excel 2011 worksheet. 我有一个试图在Excel 2011工作表中使用的自定义公式。 The idea is to use some custom logic to calculate the average of a range of cells.
想法是使用一些自定义逻辑来计算一系列单元的平均值。 When I step through the debugger in Excel, the formula runs perfectly.
当我逐步调试Excel中的调试器时,公式可以完美运行。
However, when I put the formula in a cell on the worksheet (ie =SuperAverage(K4:K17)
), I get either 0 or #VALUE
. 但是,当我将公式放入工作表中的单元格中时(即
=SuperAverage(K4:K17)
),我将得到0或#VALUE
。 I am running the formula on the same exact ranges as I do when I step through the debugger yet I am getting different answers. 我正在逐步调试程序时,在相同的精确范围内运行公式,但得到的答案却有所不同。
Why is this happening? 为什么会这样呢? I have tried manually recalculating the cells but the errors remain.
我尝试手动重新计算单元格,但错误仍然存在。
Function SuperAverage(r) As Double
Dim total_sum, total_count As Integer
total_sum = 0
total_count = 0
For Each c In r.Cells
If Not IsHidden(c) And c.value <> 0 Then 'IsHidden is another custom function irrelevant to this question
total_count = total_count + 1
total_sum = total_sum + c.value
End If
Next
If total_count = 0 Then
SuperAverage = 0
Else
SuperAverage = total_sum / total_count
End If
End Function
'test function to step through the debugger
Function Test()
Dim r As range
Set r = Worksheets("Hours").range("K4:K17")
Dim result As Double
result = SuperAverage(r)
End Function
Aye so I changed a few things, declaring variables explicitly etc and get this: 是的,所以我更改了几件事,显式声明了变量等,并得到了这一点:
Option Explicit
Function SuperAverage(ByVal r As Range) As Double
Dim total_sum As Integer
Dim total_count As Integer
total_sum = 0
total_count = 0
Dim c As Variant
For Each c In r.Cells
If Not IsHidden(c) And c.Value <> 0 Then 'IsHidden is another custom function irrelevant to this question
total_count = total_count + 1
total_sum = total_sum + c.Value
End If
Next
If total_count = 0 Then
SuperAverage = 0
Else
SuperAverage = total_sum / total_count
End If
End Function
Which seems to calculate the average fine for me. 这似乎为我计算了平均罚款。 I wonder if it might be a hard to debug error caused by the compiler inferring variable types.
我想知道由编译器推断变量类型引起的错误调试是否很困难。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.