Excel VBA:不能 Sum() over Range()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5829882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 13:08:05  来源:igfitidea点击:

Excel VBA: cannot Sum() over Range()

excelvba

提问by reforrer

How should I fix this code because it throws: "The object doesn't support this property or method"

我应该如何修复此代码,因为它抛出:“该对象不支持此属性或方法”

Sub macro1()
    Workbooks("OUTPUT.xls").Sheets("Sheet1").Activate
    ActiveSheet.Range("B4") = _ 
    Workbooks("INPUT.xlsx").Sheets("Sheet1").Sum(Range("D40:D50"))
End Sub

Above code works fine when adjusted as:

调整为以下代码时,上面的代码工作正常:

Sub macro2()
    Workbooks("OUTPUT.xls").Sheets("Sheet1").Activate
    ActiveSheet.Range("B4") = _
    Workbooks("INPUT.xlsx").Sheets("Sheet1").Range("D40")
End Sub

However it's not acceptable solution because I want to Sum() over Range() as described in macro1().

但是,这是不可接受的解决方案,因为我想按照 macro1() 中的描述对 Range() 进行 Sum()。

采纳答案by Anders Lindahl

WorkSheetdoesn't have a sum function, try using WorksheetFunctioninstead:

WorkSheet没有 sum 函数,请尝试使用WorksheetFunction代替:

Sub macro1() 
    Workbooks("OUTPUT.xls").Sheets("Sheet1").Activate
    ActiveSheet.Range("B4") = _
        Application.WorksheetFunction.Sum(Workbooks("INPUT.xlsx").Sheets("Sheet1").Range("D40:D50"))
End Sub 

回答by Arnoldiusss

try

尝试

ActiveSheet.Range("B4").Formula = Application.WorksheetFunction.Sum(Range("D40:D50"))