gpt4 book ai didi

excel - 查找前两行VBA的总和

转载 作者:行者123 更新时间:2023-12-04 21:51:37 24 4
gpt4 key购买 nike

我正在尝试一个简单的求和 vba 代码。我想在定义的范围内找到前 2 行的总和。但是,我在运行以下代码时收到 #VALUE 错误返回:

Function test(prices As Range)

test = prices.Rows.Value + prices.Rows.Offset(1, 0).Value

End Function

请指教,非常感谢!

最佳答案

Value Rows 时只会查看单个单元格正在查看您范围内的所有行,因此您的代码正在尝试添加一个单元格,该单元格认为它是整行并返回计算机相当于耸了耸肩。

尝试使用:

Function test(prices As Range) As Double
test = Application.WorksheetFunction.Sum(prices.Rows(1).Resize(2))
End Function

它将查看您给它的任何范围的前两行。

这也可行 - 现在查找不确定是否省略了 Worksheetfunction少量:
Function test(prices As Range) As Double
test = Application.Sum(prices.Rows(1).Resize(2))
End Function

关于excel - 查找前两行VBA的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52818874/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com