我是VBA新手,我正试图计算过去一个月的一些数据的总和。下面是我的代码:
Function Monthly_volume_given_velocity() As Double
Dim velocity As Double
Dim ind_vit As Integer
Dim ind_today As Integer
velocity= ActiveCell.Offset(0, -1).Value
With Sheets("2022")
ind_vel = .Range("A5:A13").Find(What:=velocity).Row
Dim rng As Range
rng = .Rows("1").Find(What:=Date, LookIn:=xlFormulas)
If Not rng Is Nothing Then ind_today = rng.Column: Debug.Print ind_today
Monthly_volume_given_velocity = Application.WorksheetFunction.Sum(.Range(.Cells(ind_vel , ind_today - 30), .Cells(ind_vel , ind_today )))
End With
End Function
使用这段代码时,我得到了一个#Value!
错误,尽管在调试模式下运行它时,我得到了Monthly_volume_given_velocity
的正确值。
用预期值替换代码中的ind_today
不会出错,所以我猜它来自Range.Find
过程。我从另一个线程获得了If Not rng...
过程,但它似乎没有解决这个问题。
有人知道我哪里做错了吗?
2条答案
按热度按时间xdnvmnnf1#
当UDF在遇到任何错误时运行,它将返回一个错误,该错误在页面中显示为
#VALUE
。现在很难说函数中到底是什么失败了,但我看到了一些问题:
ActiveCell
(或ActiveSheet
)。Excel处理重新计算,如果包含公式的单元格不活动,则可以触发重新计算。将单元格作为参数传递。Find
可能很棘手,请参见Range.Find on a Date That is a Formula。我建议您手动搜索日期。Set
。int_today
将为0,并且下面的Sum
将获得一个无效的范围作为参数。我修改了你的函数。当然,
Debug.Print
-语句可以在函数工作后删除。正如前面所说,你必须在公式中将单元格作为参数传递,例如=Monthly_volume_given_velocity(B1)
。vjrehmav2#
范围是一个对象,因此必须使用Set进行定义,因此必须
加上一条关于.Find方法的注解,以防您不知道:参数(LookIn、LookAt等)是静态的,也就是说,如果没有在新调用中定义,则会从上一次调用中记住它们。这也包括使用用户界面中的“查找”对话框。为了安全起见,每次调用时都应提供所有参数。