VBA: Add Suffix to Duplicate Value within Column

落花浮王杯 提交于 2019-12-14 03:08:46

问题


I hope you can help me resolving an issue in VBA with regard to adding a suffix to duplicate values. I have a range that looks like the following:

COL A:
000049
000050
000051
000052 (duplicate)
000052 (duplicate)
000053
000054

What I want to achieve is that once there is a duplicate in Column A, it adds a suffix to both numbers. I've only managed (using a loop) to set it for one of the fields.

Conditions:

  1. If there is a duplicate, all duplicates must have a suffix;
  2. If there is a new duplicate (let say we have 000052A and 000052B and we now move to 000061), the suffix count has to restart back from A.

Your help is really much appreciated!


回答1:


You can just do this with a formula:

=A1 & IF(COUNTIF(A:A,A1)>1, MID("ABCDEFGHIJK", COUNTIF($A$1:A1, A1), 1),"")

And copy down.

That says "Grab the value from A1 and if there is a duplicate anywhere in column A then go and get the character from string "ABCDEFGHIJK" that corresponds to how many times this value has appeared already in the list (from A1 to whatever row we are on)"



来源:https://stackoverflow.com/questions/50158120/vba-add-suffix-to-duplicate-value-within-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!