列中唯一条目的序列号

Serial for unique entries in a column

您好,我有特定的列 Header 'SHOE',其中包含重复的数字 ~50000 我希望相邻的列具有每个唯一数字的序列号。可能会出现同一鞋号在整个列中多次出现的情况。

进一步的宏应该只 运行 如果 SHOE 列存在。

谁能帮忙做个宏?

您需要额外的专栏。 在 E2 中写

=MATCH(D2,D:D,0)

找到每个 SHOE 的第一个实例

然后在新列 (F2) 中写入:

=SUMPRODUCT(--(E2>$E:$E2),1/COUNTIF($E:$E2,$E:$E2))+1

这将为您提供所需的序列号。

这是VBA解决方案:

Sub SHOP_Serial()
Dim mtc As Long

On Error Resume Next

For i = 2 To ActiveSheet.Range("D65536").End(xlUp).Row
mtc = 0
mtc = WorksheetFunction.Match(Range("D" & i), Range("D1:D" & i), 0)
    If Cells(mtc, "E").Value = 0 Then
        Cells(i, "E").Value = WorksheetFunction.Max(Range("E1:E" & i - 1)) + 1
    Else
        Cells(i, "E").Value = Cells(mtc, "E").Value
    End If

Next i

End Sub

这将根据 SHOE header:

列中的数据将结果放入第一个空列
Sub SHOE_Serial()
Dim mtc As Long
Dim shoe As Long
Dim LastColumn As Long

On Error Resume Next

LastColumn = ActiveSheet.UsedRange.Columns(ActiveSheet.UsedRange.Columns.Count).Column + 1
shoe = WorksheetFunction.Match("SHOE", Range("A1:IV1"), 0)

For i = 2 To ActiveSheet.Cells(65536, shoe).End(xlUp).Row
mtc = 0
mtc = WorksheetFunction.Match(Cells(i, shoe), Range("A1:A" & i).Offset(, shoe - 1), 0)
    If Cells(mtc, LastColumn).Value = 0 Then
        Cells(i, LastColumn).Value = WorksheetFunction.Max(Range("A1:A" & i).Offset(, LastColumn - 1)) + 1
    Else
        Cells(i, LastColumn).Value = Cells(mtc, LastColumn).Value
    End If

Next i

End Sub