Number duplicate values VBA

sofas

Well-known Member
Joined
Sep 11, 2022
Messages
559
Office Version
  1. 2021
  2. 2019
Platform
  1. Windows
Welcome . I have this code to bring the values of column E to F. Numbering duplicate values in parentheses works well. I want to add a condition to fetch data provided that there is a value in column M, or ignore the numbering of duplicate values when checking that there is no corresponding value in column M.


VBA Code:
Sub test()


Dim r As Range:         Set r = ActiveSheet.Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim Res() As Variant:   ReDim Res(1 To UBound(AR), 1 To 1)
With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If Not .Exists(AR(i, 1)) Then
            .Add AR(i, 1), 1
            Res(i, 1) = AR(i, 1)
        Else
            .Item(AR(i, 1)) = .Item(AR(i, 1)) + 1
            Res(i, 1) = AR(i, 1) & " (" & .Item(AR(i, 1)) & ")"
        End If
    Next i

        r.Offset(, 1).Value2 = Res
End With

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Is this the kind of output you're after?

MXL Number Dupes.xlsm
EFGHIJKLM
6Data 1ResultData 2
7AAA
8BBB
9CCC
10DDD
11EEE
12FFF
13GGG
14A
15BB (2)B
16C
17AA (2)A
18BB (3)B
19CC (2)C
20HHH
21III
22YYY
23TTT
Sheet2
 
Upvote 1
Is this the kind of output you're after?

MXL Number Dupes.xlsm
EFGHIJKLM
6Data 1ResultData 2
7AAA
8BBB
9CCC
10DDD
11EEE
12FFF
13GGG
14A
15BB (2)B
16C
17AA (2)A
18BB (3)B
19CC (2)C
20HHH
21III
22YYY
23TTT
Sheet2
Yes exactly. It is recommended that the value be copied to column (F) and ignored during numbering when cell( M )is empty. If not, this result can be used as well
 
Upvote 0
VBA Code:
Sub test()

Dim r As Range:         Set r = ActiveSheet.Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim BR() As Variant:    AR = r.Offset(, 8).Value2
Dim Res() As Variant:   ReDim Res(1 To UBound(AR), 1 To 1)
With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If VBA.Len(BR(i, 1)) > 0 Then
        If Not .Exists(AR(i, 1)) Then
            .Add AR(i, 1), 1
            Res(i, 1) = AR(i, 1)
        Else
            .Item(AR(i, 1)) = .Item(AR(i, 1)) + 1
            Res(i, 1) = AR(i, 1) & " (" & .Item(AR(i, 1)) & ")"
        End If
        End If
    Next i

        r.Offset(, 1).Value2 = Res
End With

End Sub
 
Upvote 0
VBA Code:
Sub test()

Dim r As Range:         Set r = ActiveSheet.Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim BR() As Variant:    AR = r.Offset(, 8).Value2
Dim Res() As Variant:   ReDim Res(1 To UBound(AR), 1 To 1)
With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If VBA.Len(BR(i, 1)) > 0 Then
        If Not .Exists(AR(i, 1)) Then
            .Add AR(i, 1), 1
            Res(i, 1) = AR(i, 1)
        Else
            .Item(AR(i, 1)) = .Item(AR(i, 1)) + 1
            Res(i, 1) = AR(i, 1) & " (" & .Item(AR(i, 1)) & ")"
        End If
        End If
    Next i

        r.Offset(, 1).Value2 = Res
End With

End Sub
Excellent. I made a slight modification to the code to get the desired result. Is it possible to add that when checking the blankness of column M, the value of column E is copied to F without numbering?
 
Upvote 0
Give this a shot.

VBA Code:
Sub test()
Dim r As Range:         Set r = ActiveSheet.Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim ra As Range:        Set ra = ActiveSheet.Range("M7:M" & Range("M" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim CO() As Variant:    CO = ra.Value2
Dim Res() As Variant:   ReDim Res(1 To UBound(AR), 1 To 1)

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If Not .Exists(AR(i, 1)) And AR(i, 1) = CO(i, 1) Then
            .Add AR(i, 1), 1
            Res(i, 1) = AR(i, 1)
        ElseIf AR(i, 1) = CO(i, 1) Then
            .Item(AR(i, 1)) = .Item(AR(i, 1)) + 1
            Res(i, 1) = AR(i, 1) & " (" & .Item(AR(i, 1)) & ")"
        Else
            Res(i, 1) = AR(i, 1)
        End If
    Next i
    r.Offset(, 1).Value2 = Res
End With

End Sub
 
Upvote 0
Give this a shot.

VBA Code:
Sub test()
Dim r As Range:         Set r = ActiveSheet.Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim ra As Range:        Set ra = ActiveSheet.Range("M7:M" & Range("M" & Rows.Count).End(xlUp).Row)
Dim AR() As Variant:    AR = r.Value2
Dim CO() As Variant:    CO = ra.Value2
Dim Res() As Variant:   ReDim Res(1 To UBound(AR), 1 To 1)

With CreateObject("Scripting.Dictionary")
    For i = 1 To UBound(AR)
        If Not .Exists(AR(i, 1)) And AR(i, 1) = CO(i, 1) Then
            .Add AR(i, 1), 1
            Res(i, 1) = AR(i, 1)
        ElseIf AR(i, 1) = CO(i, 1) Then
            .Item(AR(i, 1)) = .Item(AR(i, 1)) + 1
            Res(i, 1) = AR(i, 1) & " (" & .Item(AR(i, 1)) & ")"
        Else
            Res(i, 1) = AR(i, 1)
        End If
    Next i
    r.Offset(, 1).Value2 = Res
End With

End Sub

Thank you all for your help. I tried mixing the codes you provided me with to get the desired result, which is as follows. What I am missing now is to show an alert message that includes the values of the columns for which the command has not been executed, since the corresponding cell in column m is empty. It is stored and shown after the code is executed.

VBA Code:
Sub test()
Dim sh As Worksheet:     Set sh = Sheet2
Dim F As Variant:            Set r = sh.Range("E7:E" & Range("E" & Rows.Count).End(xlUp).Row)
Dim arr() As Variant:        arr = r.Value2: F = r.Offset(, 8).Value2
Dim col() As Variant:        ReDim col(1 To UBound(arr), 1 To 1)
Application.ScreenUpdating = False

sh.Range("F7", Range("F" & Rows.Count).End(4)).ClearContents
With CreateObject("Scripting.Dictionary")
For i = 1 To UBound(arr)
  If VBA.Len(F(i, 1)) > 0 And (arr(i, 1)) > 0 Then
        If Not .Exists(arr(i, 1)) Then
            .Add arr(i, 1), 1
            col(i, 1) = arr(i, 1)
        Else
            .Item(arr(i, 1)) = .Item(arr(i, 1)) + 1
            col(i, 1) = arr(i, 1) & " (" & .Item(arr(i, 1)) & ")"

        End If
        End If
    Next i
        r.Offset(, 1).Value2 = col
End With
Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top