在Excel中自动平均列的列

我必须平均3列的组。

例:

Blood_Patient1_0_R1Blood_Patient1_0_R2Blood_Patient1_0_R3

平均值在一个新的列Blood_Patient1_0

同样, Blood_Patient1_3_5_R1Blood_Patient1_3_5_R2Blood_Patient1_3_5_R3

平均值在一个新的列Blood_Patient1_3_5

这个过程正在重复8组这样的列。

目前我平均使用公式: IF(ISERROR(AVERAGE(B7:D7)),"",AVERAGE(B7:D7))并自动填充21,000行。

由于列标题中有一个模式,我正在考虑将整个过程自动化。

这就是我迄今为止所认为的algorithm:

  • 0,3_5,6_25是列标题中的时间值。
  • 在每个时刻,有3个重复的R1,R2,R3作为列标题的一部分

对于timearrays[3.5h,6.25h,9.5h,11.5h,16.5h,25h,49h和156h]

创build一个新的列

对于从2到21458行

使用上述公式replicates从R1到R3的平均值

我不知道如何写在Excel中。 任何帮助,将不胜感激。

给一下去。

这个解决scheme假定你有一个连续的数据集,也就是说,你想要search的列之间没有空白。

首先,你需要包含这个function。 将其粘贴到与子例程相同的模块中。 此函数的目的是允许将每个标题中的string与子string数组进行比较,而不是由InStr函数允许的单个子string。

 Function Time_Search(strCheck As String, ParamArray anyOf()) As Boolean Dim item As Long For item = 0 To UBound(anyOf) If InStr(1, strCheck, anyOf(item)) <> 0 Then Time_Search = True Exit Function End If Next End Function 

接下来,粘贴这个子程序。 我已经假定数据集从单元格A1开始。 另外,如果列数或行数有变化,我已经允许使用dynamic范围。

 Sub Insert_Average_Columns() Dim HeaderRange As Range Dim LastRow As Long Dim c As Range Set HeaderRange = Range(Range("A1"), Range("A1").End(xlToRight)) LastRow = Range("A" & Rows.Count).End(xlUp).Row For Each c In HeaderRange.Cells If Right(c.Value, 2) = "R3" Then If Time_Search(c.Value, "3_5", "6_25", "9_5", "11_5", "16_5", "25", "49", "156") Then c.Offset(0, 1).EntireColumn.Insert c.Offset(0, 1) = "Average of " & Left(c.Value, Len(c.Value) - 3) c.Offset(1, 1).FormulaR1C1 = "=IFERROR(AVERAGE(RC[-3]:RC[-1]),"""")" c.Offset(1, 1).AutoFill Range(c.Offset(1, 1).Address, Cells(LastRow, c.Offset(1, 1).Column)) End If End If Next c End Sub 

数据有一个问题。 如果您希望程序为T = 25插入一个平均值列,那么对于T中包含string“25”的所有列,将会这样做。 如果有T = 8.25,10.25,15.25等,这些都会有平均值。 唯一的方法是将更多的标题string包含在参数数组中,但是我认为你将会处理一个variablesBlood_Patient ID,所以可能不是一个选项。