首页 文章

自动平均excel中的列集

提问于
浏览
1

我必须平均设置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多行 .

由于列 Headers 中有一个模式,我正在考虑自动化整个过程 .

这就是我迄今为止在算法方面的想法:

  • 0,3_5,6_25是列 Headers 中的时间值 .

  • 在每个时刻,有3个重复的R1,R2,R3作为列 Headers 的一部分

对于时间数组[3.5h,6.25h,9.5h,11.5h,16.5h,25h,49h和156h]使用上面的公式为从R1到R3的重复的平均值创建一个新列,从2到21458

我不知道如何用excel写这个 . 任何帮助,将不胜感激 .

1 回答

  • 0

    放手一搏 .

    此解决方案假定您有一个连续的数据集,即您希望搜索的列之间没有间隙 .

    首先,您需要包含此功能 . 将其粘贴到与子例程相同的模块中 . 此函数的目的是允许将每个 Headers 中的字符串与子字符串数组进行比较,而不是 InStr 函数允许的单个子字符串 .

    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开始 . 此外,如果列数或行数发生变化,我已经允许动态范围 .

    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包含字符串“25”的所有列执行此操作 . 如果有T = 8.25,10.25,15.25等,则这些都将应用平均值 . 绕过它的唯一方法是在参数数组中包含更多的 Headers 字符串,但我认为你将处理一个变量Blood_Patient ID,这可能不是一个选项 .

相关问题