Excel 更好的循环方法?工作,但看起来不雅观

Excel 更好的循环方法?工作,但看起来不雅观,excel,vba,for-loop,Excel,Vba,For Loop,我有一个包含多列的大型数据表,其中包含的数据大部分是三重结果。每行包含来自每个主题的一个数据点的结果。大多数受试者有三个重复结果,但在某些情况下,只有一个或两个。工作表在主题id列上排序(这是分配给for循环中使用的变量rng的我的命名范围) 此循环测试范围“rng”(设置为包含受试者id的工作表中的命名范围)中的“targetcell”是否找到任何受试者重复值或重复值的底行,然后在新插入的列中生成平均值: Set rng = Range("clonesptid") col

我有一个包含多列的大型数据表,其中包含的数据大部分是三重结果。每行包含来自每个主题的一个数据点的结果。大多数受试者有三个重复结果,但在某些情况下,只有一个或两个。工作表在主题id列上排序(这是分配给for循环中使用的变量rng的我的命名范围)

此循环测试范围“rng”(设置为包含受试者id的工作表中的命名范围)中的“targetcell”是否找到任何受试者重复值或重复值的底行,然后在新插入的列中生成平均值:

Set rng = Range("clonesptid")

col = ActiveCell.Column
ActiveCell.Offset(0, 1).EntireColumn.Insert
anchor = col - rng.Column
'MsgBox "cell to test is " & rng(1)

'debugging message box to check where the ptid range is
'MsgBox "Range for ptID is " & rng.Column & " and the active cell address is " & ActiveCell.Address & " and the activecell col is " & anchor


For Each cell In rng
'uncomment the line below to check the cell addresses
'    str = str & Cell.Address & " contains " & Cell.Value & "(above=" & Cell.Offset(-1, 0).Value & " below=" & Cell.Offset(1, 0).Value & vbNewLine
'    MsgBox "What is our test value?" & vbNewLine & cell.Value
    
    If IsEmpty(cell.Value) = True Then Exit For
    
    targetcell = cell.Value
    If cell.Row > 2 Then twoup = cell.Offset(-2, 0).Value
    If cell.Row > 1 Then oneup = cell.Offset(-1, 0).Value
    onedown = cell.Offset(1, 0).Value
    
    If IsEmpty(targetcell) = False Then
        If cell.Row = 1 Then
            'adds title with means to first header row
            Cells(1, col + 1).Value = Cells(1, col).Value & " mean"
        ElseIf cell.Row = 2 And targetcell <> oneup And targetcell <> onedown Then
        'test the first value, if unique mean = the value of the cell
            cell.Offset(0, anchor + 1).Value = cell.Offset(0, anchor).Value
        ElseIf targetcell <> oneup And targetcell <> onedown Then
        'for all the rest of the cells in the range, this condition tests for singlets
            cell.Offset(0, anchor + 1).Value = cell.Offset(0, anchor).Value
        ElseIf targetcell = oneup And targetcell <> twoup And targetcell <> onedown Then
        'test for two values
            cell.Offset(0, anchor + 1).Value = (cell.Offset(0, anchor).Value + cell.Offset(-1, anchor).Value) / 2
        ElseIf targetcell = oneup And targetcell = twoup And targetcell <> onedown Then
        'test for three values
            cell.Offset(0, anchor + 1).Value = (cell.Offset(0, anchor).Value + cell.Offset(-1, anchor).Value + cell.Offset(-2, anchor).Value) / 3
        Else
        'this is the first or second replicate of duplicates or triplicates, but not yet the bottom value
            cell.Offset(0, anchor + 1).Value = ""
        End If
    End If
    
Next
Set rng=范围(“clonesptid”)
col=ActiveCell.Column
ActiveCell.Offset(0,1).entireclumn.Insert
锚定=立柱
要测试的“MsgBox”单元格为“&rng(1)
'调试消息框以检查ptid范围的位置
ptID的“MsgBox”范围为“&rng.Column&”,活动单元格地址为“&ActiveCell.address&”,活动单元格列为“&anchor”
对于rng中的每个单元
'取消注释下面的行以检查单元格地址
'str=str&Cell.Address&“contains”&Cell.Value&“(上方=“&Cell.Offset(-1,0).下方的值&=”&Cell.Offset(1,0).Value&vbNewLine
'MsgBox“我们的测试值是多少?”&vbNewLine&cell.value
如果IsEmpty(cell.Value)=True,则退出
targetcell=单元格。值
如果cell.Row>2,则twoop=cell.Offset(-2,0).Value
如果cell.Row>1,则oneup=cell.Offset(-1,0).Value
onedown=单元格偏移量(1,0).Value
如果IsEmpty(targetcell)=False,则
如果cell.Row=1,则
'将标题和方法添加到第一个标题行
单元格(1,列+1)。值=单元格(1,列)。值和“平均值”
ElseIf cell.Row=2,然后targetcell one向上和targetcell one向下
'如果唯一平均值=单元格的值,则测试第一个值
单元格偏移量(0,定位+1)。值=单元格偏移量(0,定位)。值
否则,如果目标细胞一号上升,目标细胞一号下降
'对于该范围内的所有其他单元格,此条件测试单态
单元格偏移量(0,定位+1)。值=单元格偏移量(0,定位)。值
ElseIf targetcell=one向上,targetcell 2向上,targetcell 1向下
'测试两个值
单元格偏移量(0,锚定+1)。值=(单元格偏移量(0,锚定)。值+单元格偏移量(-1,锚定)。值)/2
ElseIf targetcell=one向上,targetcell=two向上,targetcell=one向下
“测试三个值
单元格偏移量(0,锚定+1)。值=(单元格偏移量(0,锚定)。值+单元格偏移量(-1,锚定)。值+单元格偏移量(-2,锚定)。值)/3
其他的
'这是重复或三次复制的第一次或第二次复制,但还不是最底值
单元格偏移量(0,锚点+1).Value=“”
如果结束
如果结束
下一个

如果您有一个特定的操作,比如“上下查找所有相同的值”,那么最好将其移动到一个单独的方法

未经测试:

子测试仪()
Dim rng作为范围、单元格作为范围、DUP作为范围
调暗lastDup作为范围,列作为长度,锚作为长度
col=ActiveCell.Column
单元格(1,col+1).entireclumn.Insert
单元格(1,col+1)。Value=“平均值”
设置rng=范围(“clonesptid”)
锚定=立柱
设置单元格=rng.单元格(1)
当Len(单元格值)>0时执行
设置dups=DupsRange(单元格)'获取列中具有相同值的连续范围
设置lastDup=dups.Cells(dups.Cells.Count)
“在这里计算你的平均数
lastDup.Offset(0,锚定+1)=应用程序平均值(DUP.Offset(0,锚定))
设置单元格=上次重复偏移量(1,0)'下一组
环
端接头
'给定一个单元格,上下检查以找到
'连续的相同值范围
公用函数DupsRange(c作为范围)作为范围
暗cStart作为范围,cEnd作为范围
如果Len(c.Value)=0,则退出函数
设置cStart=c
设置cEnd=c
当cStart.Row>1时执行
如果cStart.Offset(-1,0).Value=c.值,则_
设置cStart=cStart.Offset(-1,0)否则退出Do
环
当cEnd.Row
谢谢!虽然这看起来可能有更多的代码行,但它确实看起来更优雅,并且可能更适用于其他数据集,例如,如果有三个以上的复制。我将尝试一下。