Assigning name for Unique and Duplicated Values in a Column

madhuchelliah

Board Regular
Joined
Nov 22, 2017
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Hello all, i have data in A column, which has unique values and duplicated values. I want to assign a name like Group 1, Group 2....
VBA has to look into all the data in A column and assign group name in D column. If any value is repeated it should be assigned with same Group name.

Thank you.
Example

Book1
ABCD
1NameXXXYYYGroup
2AAAGroup - 1
3AAAGroup - 1
4CCCGroup - 2
5DDDGroup - 3
6EEEGroup - 4
7EEEGroup - 4
8EEEGroup - 4
9FFFGroup - 5
10FFFGroup - 5
11GGGGroup - 6
12HHHGroup - 7
Sheet1
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this with a copy of your worksheet.

VBA Code:
Sub GroupNum()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("A" & Rows.Count).End(xlUp))
  For i = 1 To UBound(a)
    If Not d.exists(a(i, 1)) Then d(a(i, 1)) = "Group - " & d.Count + 1
    a(i, 1) = d(a(i, 1))
  Next i
  Range("D2").Resize(UBound(a)).Value = a
End Sub
 
Upvote 0
Solution
Try this with a copy of your worksheet.

VBA Code:
Sub GroupNum()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
 
  Set d = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("A" & Rows.Count).End(xlUp))
  For i = 1 To UBound(a)
    If Not d.exists(a(i, 1)) Then d(a(i, 1)) = "Group - " & d.Count + 1
    a(i, 1) = d(a(i, 1))
  Next i
  Range("D2").Resize(UBound(a)).Value = a
End Sub
Hello Pete, it is working as expected. Thank you.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,216,574
Messages
6,131,492
Members
449,653
Latest member
aurelius33

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