vba 无法获取工作表函数类的平均属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18164857/
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 22:41:50  来源:igfitidea点击:

Unable to get the average property of the worksheetfunction class

vbaexcel-vbaexcel

提问by Max Hui

Hi I'm trying to write vba to help me convert daily data into weekly data. I wrote this vba but said unable to get the average property of the worksheetfunction class. Is it possible someone can help me figure whats wrong? thanks!

嗨,我正在尝试编写 vba 来帮助我将每日数据转换为每周数据。我写了这个 vba 但说unable to get the average property of the worksheetfunction class. 有人可以帮我弄清楚出了什么问题吗?谢谢!

Sub CopyData()
Dim z As Integer

  For z = 0 To 2000

   Set Rng1 = ActiveSheet.Range("D5:D11").Offset(7 * z, 0)

    Range("runningagain").Offset(z, 0) = Application.WorksheetFunction.Average(Rng1)

    Do Until IsEmpty(ActiveCell.Value)
      ActiveCell.Offset(1, 0).Select
    Loop

  Next z

End Sub

回答by user3357963

The average function in Excel will produce a #DIV/0! error if the cells are empty so you need to check rng1is not empty.

Excel 中的平均函数将产生 #DIV/0!如果单元格为空,则错误,因此您需要检查是否rng1为空。

As you are taking the average then it's best to use Count instead of CountA

当您取平均值时,最好使用 Count 而不是 CountA

If Application.WorksheetFunction.Count(Rng1) > 0 Then
    Range("runningagain").Offset(z, 0) = Application.WorksheetFunction.Average(Rng1)
End If

Also, you may need to define Range("runningagain")properly, do you mean Range(runningagain)?

另外,您可能需要Range("runningagain")正确定义,您的意思是Range(runningagain)