Adding the count of repeating values

dshafique

Board Regular
Joined
Jun 19, 2017
Messages
171
Hi everyone, I would like your help on figuring out an issue I am facing. I have built a macro, where I need to format the source data and map it and transfer it to a template. One of the fields I have to map requires that I put an underscore and a number after the record, the number being the duplicate number. for instance, if the data set has multiple same records, I need to map it like "record_1", "record_2" etc etc. I can understand the logic of how to do this if I'm coding in python or something, but cannot execute my thoughts in vba, any help would be appreciated. see below an example of sample data.

recordData
Appledata
Orangedata
Orangedata
Kiwidata
Appledata
Appledata
Appledata
Kiwidata
Bananadata
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
I found this example here. If I understand you correctly...

You'll notice that as the formula counts down, it automatically increased the range it's counting to get numbers greater than 1.

VBA Testing.xlsm
IJK
1RecordDataCount
2Appledata1
3Orangedata1
4Orangedata2
5Kiwidata1
6Appledata2
7Appledata3
8Appledata4
9Kiwidata2
10Bananadata1
Indirect-Dates
Cell Formulas
RangeFormula
K2:K10K2=COUNTIF($I$2:$I2,$I2)
 
Upvote 0
Thank you, this is exactly in the direction of what I'm looking for. Is there a way for me to do this in the VBA code without the need of the extra helper columnn with the formula?

currently I have a code which will take the value in the record column and add_1 to it. currently I just hardcoded _1, but would like to have it be _X where x will be the number it is

Thanks
 
Upvote 0
Is there a way for me to do this in the VBA code without the need of the extra helper columnn with the formula?
I'm confident there is, but my VBA skills are lacking to say the least. I try to avoid adding new columns as well, especially with large datasets in the thousands, as formulas constantly updating with new changes slows down Excel.

I suspect that the cell value in the 'Record' column would need to be renamed to reflect the number curently in the 'Count' column. You may not want your macro renaming all of the values in the column, and just the new one being added.

This might be achieveable using a Worksheet_Change sub routine. Hopefully this helps you look for a solution if the formula/helper column isn't optimal.

However, until a VBA expert in this forum comes along, you could use this to combine the results and then have your macro use that combined value for the timebeing.

I'm providing two examples: TEXTJOIN and CONCAT, depending on which version you are using.
If you use either of the two examples, you don't need my column 'C'. I'm combining the value in your 'Record' column with that formula from 'C' so you only need one helper column.

If a MACRO is your desired approach, then don't mark this thread as SOLVED until a VBA expert provides one. Or, you can take this thread and create a new one converting this Formula-based solution to a Macro-based solution.

Either way, best of luck to you.

VBA Testing.xlsm
ABDE
1Requires Office 365Any Office Version
2Combined using TEXTJOINCombined using CONCAT
3RecordDataRecord_CountRecord_Count
4AppledataApple_1Apple_1
5OrangedataOrange_1Orange_1
6OrangedataOrange_2Orange_2
7KiwidataKiwi_1Kiwi_1
8AppledataApple_2Apple_2
9AppledataApple_3Apple_3
10AppledataApple_4Apple_4
11KiwidataKiwi_2Kiwi_2
12BananadataBanana_1Banana_1
Count-Dupes
Cell Formulas
RangeFormula
D4:D12D4=TEXTJOIN("_",1,$A4,COUNTIF($A$4:$A4,$A4))
E4:E12E4=CONCAT($A4,"_",COUNTIF($A$4:$A4,$A4))
 
Upvote 0
Is there a way for me to do this in the VBA code without the need of the extra helper columnn with the formula?
I just realized something while looking for a vba solution...

Renaming the values in the Record column will change the cell value and thus change the Count number of duplicates.

For example, if a macro renames the first Apple to Apple_1, then the next Apple in the list will not be identified as a duplicate, such as Apple_2.

It looks like the helper column is necessary, UNLESS the macro strips the "_#" to check for duplicates... and that sounds insanely difficult... in my opinion of course. :unsure:
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

I guess helper column is needed
No, it shouldn't be needed.
Try this with a copy of your workbook (you may need to edit the column as I have assumed the "record" data in column A.

VBA Code:
Sub Add_Suffix()
  Dim d As Object
  Dim a As Variant
  Dim i As Long
  
  Set d = CreateObject("Scripting.Dictionary")
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    a = .Value
    For i = 1 To UBound(a)
      d(a(i, 1)) = d(a(i, 1)) + 1
      a(i, 1) = a(i, 1) & "_" & d(a(i, 1))
    Next i
    .Value = a
  End With
End Sub

Before:
dshafique.xlsm
AB
1recordData
2Appledata
3Orangedata
4Orangedata
5Kiwidata
6Appledata
7Appledata
8Appledata
9Kiwidata
10Bananadata
Sheet1


After:
dshafique.xlsm
AB
1recordData
2Apple_1data
3Orange_1data
4Orange_2data
5Kiwi_1data
6Apple_2data
7Apple_3data
8Apple_4data
9Kiwi_2data
10Banana_1data
Sheet1
 
Upvote 1

Forum statistics

Threads
1,223,871
Messages
6,175,099
Members
452,612
Latest member
MESTeacher

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