在excel里面如何编辑累加计数的宏

来源:百度知道 编辑:UC知道 时间:2024/04/29 03:47:25
如果我数据的第一列为W001A、W001A、W001A、W001B、W001C这5个数字,如何用公式计算出对应的为1、2、3、1、1?请高手帮忙,谢谢!

用VBA吧,如果数据在A列中.按alt+F11复制以下代码进去:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Row = 1 And Target.Column = 2 Then
Cells(1, 2) = 1
Dim a, b, c, d, n As Integer
n = Range("A65536").End(xlUp).Row
For a = 2 To n
For b = a - 1 To 1 Step -1
If Cells(a, 1) = Cells(b, 1) Then
c = 1
d = b
Exit For
Else
c = 0
d = 0
End If
Next b
If c = 1 Then Cells(a, 2) = Cells(d, 2) + 1 Else Cells(a, 2) = 1
Next a
End If
End Sub
注意,选择B1单元格即可激活排序.

=countif($a$1:a1,a1)