Attached file contains different formulas than these from post #1, so code has to be changed accordingly:
Sub test()
Dim i As Long, j As Long
i = 3
For j = 3 To Cells(Rows.Count, "B").End(xlUp).Row
If Cells(j, "A") = "cond" Then i = j
Cells(j, "C").FormulaR1C1 = "=countif(R" & i & "C2:RC2,RC2)"
Next j
End Sub
Once you run it appropriate range will be filled with formulas. delete contents of column C and run the macro.
If what you look for is UDF (vba to be used the same way you use regular formula) here is the sample code and usage is presented in attached file:
Function countif_from_cond(input_range As Range, counted As Variant, cond As Variant) As Long
Dim myarr, i As Long, j As Long
myarr = input_range.Value
i = UBound(myarr)
Do
If myarr(i, 2) = counted Then j = j + 1
i = i - 1
Loop Until myarr(i + 1, 1) = cond Or i < 0
countif_from_cond = j
End Function
it is written just as regular formula in C3 and copied down.
Bookmarks