我需要一个公式或宏来让 Excel 执行下列操作:
例如,识别重复项并保留第一个值
2
2
2
2
2
2
应该成为
2
2.1
2.2
2.3
2.4
2.5
这应该对特定列中的所有重复项执行,在本例中为 B 列。
到目前为止,我尝试过的方法是使用如下公式
=IF(COUNTIF($B$1:$B$5000,B1)>1,B1& " (" & COUNTIF(B$1:B1,B1) & ")",B1)
这不起作用,而且在 VB 中编写宏并不是我的强项。我测试的宏也不起作用:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim dataRng As Range
Dim dataArr() As Variant, output() As String
Dim y As Long, i As Long, j As Long, tmpcount As Long
Set dataRng = Range("B1").Resize(Me.UsedRange.Rows.Count, 1)
If Not Intersect(Target, dataRng) Is Nothing Then
dataArr = dataRng.Value
ReDim output(1 To UBound(dataArr, 1), 1 To 1)
For y = 1 To UBound(dataArr, 1)
If Right(dataArr(y, 1), 1) = ")" Then
dataArr(y, 1) = Left(dataArr(y, 1), InStr(dataArr(y, 1), " (") - 1)
End If
Next y
For i = 1 To UBound(dataArr, 1)
tmpcount = 0
output(i, 1) = dataArr(i, 1)
For j = 1 To UBound(dataArr, 1)
If dataArr(i, 1) = dataArr(j, 1) Then
tmpcount = tmpcount + 1
If j = i And tmpcount > 1 Then
output(i, 1) = dataArr(i, 1) & " (" & tmpcount & ")"
Exit For
End If
If j > i And tmpcount > 1 Then
output(i, 1) = dataArr(i, 1) & " (" & tmpcount - 1 & ")"
Exit For
End If
End If
Next j
Next i
Call printoutput(output, dataRng)
End If
End Sub
Private Sub printoutput(what As Variant, where As Range)
Application.EnableEvents = False
where.Value = what
Application.EnableEvents = True
End Sub
有人知道这里该做什么吗?提前感谢您的回复。
答案1
答案2
尝试拆分响应以查看错误原因(我无法打开您的链接 de.tinypic.com/r/mja83/9 来查看发生了什么)。例如,在 C2 中写入 =countif($A$1:A1,A2),尝试使用 fx 或插入公式以使用您语言中的相应公式,也许列表分隔符是 ; 而不是 ,在您的计算机设置中。对于 D2 中的 =If(C2=0,"",",") 也是如此,然后在 B2=A2 & D2 & C2 中如果一切正确,请再次组合公式。Gary 的公式是正确的并且有效