1
我有一個數字列表(50,000+),我需要在每個唯一組的末尾添加一個標識。該ID的格式爲「 - ###」。向數字列表添加唯一標識 - VBA
151022
151022
151020
150922
150715
150911
151014
151021
151020
151019
151019
151019
151020
我需要它來打印這樣
151022-001
151022-002
151020-001
150922-001
150715-001
150911-001
151014-001
151021-001
151020-002
151019-001
151019-002
151019-003
151020-002
我現在有這樣的代碼,我發現並略作修改。如果我能夠在-002開始計算唯一值,那麼我相信這會解決它。
Option Explicit
Sub test()
Dim uniqueCounter As New Scripting.Dictionary
Dim counter As Long
Dim rowCount As Long
Dim identifer As String
rowCount = ActiveCell.CurrentRegion.Rows.Count '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) = identifer & "-00" & uniqueCounter(CStr(Sheet1.Cells(counter, 1)))
Else
uniqueCounter.Add identifer, "0"
Sheet1.Cells(counter, 2) = identifer
End If
Next counter
End Sub
這是什麼當前顯示:
151022 151022
151022 151022-001
151020 151020
150922 150922
150715 150715
150911 150911
151014 151014
151021 151021
151020 151020-001
151019 151019
151019 151019-001
151019 151019-002
151020 151020-002
謝謝大家!
++是否需要編寫VBA? :) – cyboashu
@cyboashu爲什麼重新發明輪子? –
真棒,完美工作。在嘗試使用VBA之前,我嘗試了一個類似的公式,但由於某些原因,這些公式並不正確乾杯! –