在工作表中的单元格之间进行求和

时间:2015-06-17 12:09:36

标签: excel vba excel-vba

我对编程很陌生,所以这可能是一个显而易见的问题。我有一份问卷询问饮酒习惯。我正在尝试创建一个应用程序来解决所有工作表中的平均值(参与者响应)。到目前为止,我的代码允许我将工作表的总数作为一些参与者的响应,但它只会给我当前打开的工作表的平均值。我需要添加到下面的代码中以便让我获得所有答案(工作表)的总平均值。

代码:

Function PARTcounter() 'to count the open questionaires

Dim WS_Count As Integer
Dim I As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
For I = 1 To WS_Count
TBTOTpar = WS_Count
Next I
End Function

Function SUMwk11() 'finds mean for week 1
Dim ws As Worksheet
Dim rng As Range
Dim WS_Count As Integer
Dim A As Double
Dim B As Double
WS_Count = ActiveWorkbook.Worksheets.Count
For counter = 0 To WS_Count
Next
A = Application.SUM(Range("c3:i9"))
B = A / 7
TBwk1MEAN = B
End Function

1 个答案:

答案 0 :(得分:0)

你的for循环实际上并没有做任何事情......你也可以在一个sub中完成这一切(不确定你为什么要使用函数)。一旦更新TBwk1MEAN以更改每个循环

,这应该可以执行您想要的操作
Sub PARTcounter() 'to count the open questionaires
    TBTOTpar = ThisWorkbook.Worksheets.Count
End Sub
Sub SUMwk11() 'finds mean for week 1
    Dim ws As Worksheet

    For each ws in ThisWorkbook.Worksheets
        ' You'll need to update this each loop otherwise the mean from each sheet will overwrite the previous one.
        TBwk1MEAN = Application.SUM(ws.Range("c3:i9")) / 7
    Next ws
End Sub