暂时只想到了借助辅助列的办法,
1、在B1单元格输入公式:=IF(A1="a",COUNTIF(INDIRECT("$A$1:A"&ROW(A1)),"*"),""),向下填充
2、在C1单元格输入公式:=IF(A1="a",ROW(A1),""),向下填充
3、在D2单元格输入公式:=IF(B2<>"",MAX($C$1:C1),""),向下填充
4、在E2单元格输入公式:=IF(B2<>"",B2-INDIRECT("B"&D2),""),向下填充
最终E列即是最终结果,如下图所示:
Sub qqq()
For i = 1 To Range("a65536").End(xlUp).Row
k = 1
For t = i + 1 To Range("a65536").End(xlUp).Row
If Range("a" & i) <> "" Then
If Range("a" & t) = Range("a" & i) Then
Range("b" & t) = k
GoTo 10
ElseIf Range("a" & t) <> "" Then k = k + 1
End If
End If
Next t
10 Next i
End Sub