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
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