循环通过一个列的重复单元格,然后放置一个“-NUMBER”
我有一个庞大的电子表格,在其中一列中有SUPPOSED是一个唯一的标识符。 但问题是我的客户不了解“唯一性”要求的重要性。
所以我只是不想手动通过7000行,重命名这些。 我知道如何做循环,我知道如何突出重复的单元格,但我不知道如何循环通过重复,并把一个柜台后面,所以如果我有:
duplicate duplicate duplicate
它会使:
duplicate-1 duplicate-2 duplicate-3
我该怎么做呢?
如果你想坚持使用VBA,你可以在Windows Scripting Runtime中使用Dictionary对象来帮助你。
首先,设置对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
上面的代码轮stream将处理以下数据:
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
你可以做到这一点,而不诉诸于VBA。 假设列A是客户名称的半独特列(即,有时客户不止一次出现,在这种情况下,您要区分这些logging)。 使用此计算并填充表格的长度:
- 在列B中input并填充:
=MATCH(A2,A:A,)
- 在列C中,input和填充:
=IF(B2=ROW(), 1, C1+1)
- 在D列中,input并填充:
=A2 & "-" & C2
它有什么作用?
- B列告诉你每个客户的起始行。
- C列查看每个客户的起始行,并从1开始计数。
- D列只是将A&D结合起来作为唯一的标识符
你会得到像下面的表格。 之后,您可以复制和粘贴值将计算转换为一个值,删除额外的列,你就完成了。
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
不pipe顺序如何,一个列的公式都是这个公式(对于以A1开始的数据)复制下来=A1 &"-" &COUNTIF($A$1:A1,A1)