【问题标题】:VBA: Adding an identifier to values in column if there are duplicatesVBA:如果有重复,则向列中的值添加标识符
【发布时间】:2022-08-18 06:21:46
【问题描述】:

如果有重复值,我正在尝试将标识符分配给字符串的后面。

我考虑了一个带有计数器的 for 循环,但它只是在每个单元格中给了我一个数字序列。

还有其他方法可以解决这个问题吗?

Sub Macro1()
For i = 1 To 10
    For N = 1 To 10
        If Worksheets(\"sheet1\").Range(\"A\" & i) = Worksheets(\"sheet1\").Range(\"A\" & N) Then
            Worksheets(\"sheet1\").Range(\"A\" & i) = Worksheets(\"sheet1\").Range(\"A\" & i) & counter
            counter = counter + 1
        End If
    Next N
Next i
End Sub

  • =IF(COUNTIF($A$2:$A$11,A2)>1,A2 & \"_\" & COUNTIF($A$2:A2,A2),A2)
  • 谢谢蒂姆!有没有办法可以将它实现到数组?

标签: excel vba


【解决方案1】:

可能还有其他解决方案,但这是我真正想出的。 不要忘记将此引用添加到您的项目中:工具 -> 参考 -> Microsoft 脚本运行时 -> 勾选复选框

Sub Macro1()
' Add reference to project:
' Tools -> References -> Microsoft Scripting Runtime -> tick checkbox
Dim dDict As New Scripting.Dictionary
Dim rngInput As Range
Dim sOutputCol As String
Dim lRow As Long, lCount As Long

    
    With ThisWorkbook.Worksheets("SHEET_NAME")  ' SHEET_NAME: the name of the sheet where input is
        Set rngInput = .Range("A1:A10")         ' A1:A10: the range on the sheet where input is
        sOutputCol = "C"                        ' sOutputCol: output column's letter
        
        For lRow = rngInput.Rows(1).Row To rngInput.Rows(rngInput.Rows.Count).Row
            If dDict.Exists(rngInput.Cells(lRow, 1).Value2) Then
                lCount = dDict(rngInput.Cells(lRow, 1).Value2)
                dDict.Remove rngInput.Cells(lRow, 1).Value2
                dDict.Add rngInput.Cells(lRow, 1).Value2, lCount + 1
            Else
                dDict.Add rngInput.Cells(lRow, 1).Value2, 1
            End If
            .Cells(lRow, sOutputCol).Value2 = rngInput.Cells(lRow, 1).Value2 & "_" & dDict(rngInput.Cells(lRow, 1).Value2)
        Next lRow
    End With
End Sub

【讨论】:

  • 谢谢彼得!有没有使用字典的替代方法?
  • 字典是一个快速的工具,你应该考虑使用它!特别是如果您的输入范围具有大量唯一值。有什么东西阻止你使用它吗?
【解决方案2】:

这是一个稍微不同的 Dictionary 方法,它为您提供最终值的数组。

Sub Tester()
    Dim arr, r As Long, dict As Object, rng As Range, v, tmp
    Set dict = CreateObject("scripting.dictionary")
    
    Set rng = ActiveSheet.Range("B1:B20")
    arr = rng.Value                 'get the input values in an array
    For r = 1 To UBound(arr, 1)     'loop over all the values
        v = arr(r, 1)
        If Not dict.exists(v) Then  'new value?
            dict.Add v, Array(r, 1) 'store row for the first occurence and start the count
        Else
            tmp = dict(v)       'not new value: pull array from dict
            tmp(1) = tmp(1) + 1 'increment count for this value
            If tmp(1) = 2 Then arr(tmp(0), 1) = arr(tmp(0), 1) & "_1" 'second instance? Flag the first occurence also...
            arr(r, 1) = arr(r, 1) & "_" & tmp(1)   'flag the n'th instance
            dict(v) = tmp                          're-store the array
        End If
    Next r
    
    rng.Offset(0, 2).Value = arr 'output the (updated) array
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 2022-11-18
    • 2012-01-29
    相关资源
    最近更新 更多