Add a reference cell formula in to a code

后端 未结 1 555
囚心锁ツ
囚心锁ツ 2021-01-29 06:41

enter image description here

This is what would be the output

I have this formula in cell \"C1\" To average the values in column2 with respecte

相关标签:
1条回答
  • 2021-01-29 07:03

    Here is one way to do C1's calculation is VBA:

    Sub DoingC1Job()
        Dim s As String, s2 as string
        s = "=IF(A2<>A1,AVERAGEIF(A2:INDEX($A2:INDEX(A:A,MATCH(1E+99,A:A)+1),MATCH(TRUE,(INDEX($A2:INDEX(A:A,MATCH(1E+99,A:A)+1)<>A2,)),0)),A2,B2:INDEX($B2:INDEX(B:B,MATCH(1E+99,A:A)+1),MATCH(TRUE,(INDEX($A2:INDEX(A:A,MATCH(1E+99,A:A)+1)<>A2,)),0))),??)"
        s = Replace(s, "??", Chr(34) & Chr(34))
        s2 = Evaluate(s)
        MsgBox s2
    End Sub
    

    The ?? is just an easy way to get the double-quotes into the string.

    As improved by Leviathan:

    Sub DoingC1Job()
        Dim s As String, v As Variant
        s = "=IF(A2<>A1,AVERAGEIF(A2:INDEX($A2:INDEX(A:A,MATCH(1E+99,A:A)+1),MATCH(TRUE,(INDEX($A2:INDEX(A:A,MATCH(1E+99,A:A)+1)<>A2,)),0)),A2,B2:INDEX($B2:INDEX(B:B,MATCH(1E+99,A:A)+1),MATCH(TRUE,(INDEX($A2:INDEX(A:A,MATCH(1E+99,A:A)+1)<>A2,)),0))),"""")"
        v = Evaluate(s)
        MsgBox v
    End Sub
    
    0 讨论(0)
提交回复
热议问题