vba Excel中VBA的平均值

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

Average in VBA in Excel

excelvbaexcel-vbaaverage

提问by Harshit Mehra

I am new to VBA in excel . I have a range of data like 11000 numbers .i want that it calculates the average of first 60 then next 60 till the end . I did some programming but that isint working . So can someone please help me with it .

我是 excel 中的 VBA 新手。我有一系列数据,如 11000 个数字。我希望它计算前 60 个的平均值,然后是下一个 60 个,直到最后。我做了一些编程,但这不是工作。所以有人可以帮我解决这个问题。

Sub Hourlyaverage()
Dim i As Long, j As Long, k As Long, l  As Long, m As Long

Sheets("DUT1_Test51_excel").Select

i = 3
j = 3
k = 63

Do While Cells(i, 12).Value <> ""

    l = Cells(i, 12).Value
    m = Cells(k, 12).Value

    Cells(j, 20).Value = [Average (l : m)]
    i = i + 60
    j = j + 1
    k = k + 60
Loop


End Sub

回答by 314UnreadEmails

Look closely at what you're trying to do in this code:

仔细查看您在此代码中尝试执行的操作:

l = Cells(i, 12).Value
m = Cells(k, 12).Value

Cells(j, 20).Value = [Average (l : m)]

You're assigning a "long" value to each of land mand then calling them in the average function as if they were references to a cell.

你分配一个“长”值给每个lm,然后调用它们的平均函数,好像他们是一个单元格引用。

I suggest that you add a range variable, assign the locationof the data you want to average and use that as your function argument.

我建议您添加一个范围变量,指定要平均的数据的位置并将其用作函数参数。

Something like:

就像是:

Dim myRange as Range

'Loop code here
Set myRange = Range("L" & i & ":L" & k)
Cells(j, 20).Value = Application.Average(myRange)