我有一个巨大的电子表格,在其中一列中应该有一个唯一的标识符。但问题是我的客户不理解“唯一性”要求的重要性。
因此,我不想手动遍历7000行,并对它们进行重命名。我知道如何做循环,我知道如何突出显示重复的单元格,但我不知道如何循环重复的单元格,并在它们后面放置一个计数器,所以如果我有:
duplicate
duplicate
duplicate它会让你:
duplicate-1
duplicate-2
duplicate-3我该怎么做呢?
发布于 2011-10-05 22:24:23
如果你想继续使用VBA,你可以使用Windows Scripting Runtime中的Dictionary object来帮助你。
首先,设置对Windows脚本运行时的引用:

然后,执行如下所示的代码:
Option Explicit
Sub test()
Dim uniqueCounter As New Scripting.Dictionary
Dim counter As Long
Dim rowCount As Long
Dim identifer As String
rowCount = 17 'Whatever code you want to put in to calculate the last row
For counter = 1 To rowCount
identifer = Sheet1.Cells(counter, 1) 'Put whatever number of combination of cells which the row unique here (maybe it's just the one)
If uniqueCounter.Exists(identifer) Then
uniqueCounter(identifer) = CLng(uniqueCounter(CStr(Sheet1.Cells(counter, 1)))) + 1
Sheet1.Cells(counter, 2) = "Duplicate #" & uniqueCounter(CStr(Sheet1.Cells(counter, 1)))
Else
uniqueCounter.Add identifer, "0"
Sheet1.Cells(counter, 2) = "Original"
End If
Next counter
End Sub上述代码转换将处理以下数据:
1
2
3
1
1
1
3
2
1
1
2
3
12
15
3
4
15并在b栏中填写原件和重复计数,如下所示:
1 Original
2 Original
3 Original
1 Duplicate #1
1 Duplicate #2
1 Duplicate #3
3 Duplicate #1
2 Duplicate #1
1 Duplicate #4
1 Duplicate #5
2 Duplicate #2
3 Duplicate #2
12 Original
15 Original
3 Duplicate #3
4 Original
15 Duplicate #1发布于 2011-10-05 21:53:54
您可以在不借助VBA的情况下完成此操作。假设A列是客户名称的半唯一列(即,有时一个客户出现多次,在这些情况下,您需要区分这些记录)。使用此计算并填充表的长度:
在B列中,输入this并向下填充:=MATCH(A2,A:A,)
=IF(B2=ROW(), 1, C1+1)
=A2 & "-" & C2是干什么的呢?
您将得到类似于下表的内容。之后,您可以复制&PasteValue将计算转换为一个值,删除多余的列,然后就完成了。
CustName First Counter Unique
A 2 1 A-1
B 3 1 B-1
C 4 1 C-1
C 4 2 C-2
D 6 1 D-1
E 7 1 E-1
E 7 2 E-2
E 7 3 E-3
F 10 1 F-1发布于 2011-10-06 17:06:10
一个与顺序无关的单列公式是复制下来的=A1 &"-" &COUNTIF($A$1:A1,A1)公式(对于以A1开头的数据)

https://stackoverflow.com/questions/7661895
复制相似问题