Macro to create Sub-account numbers

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,585
Office Version
  1. 2021
Platform
  1. Windows
I import data and there are several account numbers in Col A on sheet "Imported data" that have the same account number that was downloaded from another program which stored the account number in one field and a sub-numbber in another field. When downloading the sub-field in not part of the download

I would like a macro to place a 01 to end of the number that appears a second time in Col A 02 to the number when it appears a second time 03 a third time etc etc eg if 7189 appears a second time 01 to be shown at the end 718901 , if it appears again 718902. If 7000 appears a second time 01 to be placed at the end 700001 a second time 700002 etc

Your assistance in this regard is most appreciated
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I import data and there are several account numbers in Col A on sheet "Imported data" that have the same account number that was downloaded from another program which stored the account number in one field and a sub-numbber in another field. When downloading the sub-field in not part of the download

I would like a macro to place a 01 to end of the number that appears a second time in Col A 02 to the number when it appears a second time 03 a third time etc etc eg if 7189 appears a second time 01 to be shown at the end 718901 , if it appears again 718902. If 7000 appears a second time 01 to be placed at the end 700001 a second time 700002 etc

Your assistance in this regard is most appreciated
If your "numbers" really are numbers, then try the following code. Else a bit of modification.
Code:
Sub jxj()
Dim a, d As Object, i As Long

Set d = CreateObject("scripting.dictionary")
a = Cells(1).CurrentRegion.Resize(, 1)

For i = 1 To UBound(a, 1)
   If Not d.exists(a(i, 1)) Then
        d(a(i, 1)) = 0
   Else
        d(a(i, 1)) = d(a(i, 1)) + 1
        a(i, 1) = a(i, 1) * 100 + d(a(i, 1))
    End If
Next i

Cells(1).CurrentRegion.Resize(, 1) = a

End Sub
 
Upvote 0
Thanks for the reply. Have avtivated your macro, but where number (think it appears as text) appears motre than once 01 , 02 not added to the end of number

See sample data below

Excel Workbook
A
77183
87183
97189
10** TOTALS FOR
11** Department: NEW VEHICLES
127183
137093
147158
15** TOTALS FOR
16** Department: SERVICE
177071
Page 1
 
Upvote 0
Thanks for the reply. Have avtivated your macro, but where number (think it appears as text) appears motre than once 01 , 02 not added to the end of number

See sample data below
...
...
OK. Modified. Just the one line about 5th from bottom
Code:
Sub kxk()
Dim a, d As Object, i As Long

Set d = CreateObject("scripting.dictionary")
a = Cells(1).CurrentRegion.Resize(, 1)

For i = 1 To UBound(a, 1)
   If Not d.exists(a(i, 1)) Then
        d(a(i, 1)) = 0
   Else
        d(a(i, 1)) = d(a(i, 1)) + 1
        a(i, 1) = a(i, 1) & "0" & d(a(i, 1))
    End If
Next i
Cells(1).CurrentRegion.Resize(, 1) = a
End Sub
 
Upvote 0
Thanks for the reply-Still not adding 01 where same number appears a second time. Would it be possible for me to email you my file? IIf so send me a PM and I will email you my file
 
Upvote 0
Thanks for the reply-Still not adding 01 where same number appears a second time. Would it be possible for me to email you my file? IIf so send me a PM and I will email you my file
Could be several reasons, mainly depending on your data setup.

If your data are in Column A then try the following modification. It it still doesn't do as you ask then it may be that your "duplicate numbers" are not duplicates. If text they may include non-printing characters such as spaces.
Code:
Sub lxxl() 'for howard

Dim a, d As Object, i As Long, rws As Long
Set d = CreateObject("scripting.dictionary")
rws = Cells(Rows.Count, "A").End(3).Row
a = Cells(1).Resize(rws)

For i = 1 To rws
    If Len(a(i, 1)) > 0 Then
        If Not d.exists(a(i, 1)) Then
            d(a(i, 1)) = 0
        Else
            d(a(i, 1)) = d(a(i, 1)) + 1
            a(i, 1) = a(i, 1) & "0" & d(a(i, 1))
        End If
    End If
Next i
Cells(1).Resize(rws) = a

End Sub
 
Upvote 0
Thanks for the info Rick.

Seems I may have been spending time on this thread (luckily not much) unnecessarily.


I know nothing about Excel Fox and don't really want to. The limited amount of Excel I do diverts me from too many other interesting things in life ...


So, quite happy to leave this one in your capable hands.


Good luck!
 
Upvote 0
Thanks for the help, it works perfectly (added sheets("import").select as I have several sheets)

I also have a sheets Fassets, that also has duplicate numbers that I need to add 01 at the end. However, the numbers are in column C. I tried copying your code to the end of the previous code and amending to suit my needs i.e Col C sheets (FASSETS), but cannot get it to work. It would be appreciated

Regards

Howard
 
Upvote 0
Hi Rick & Mirabeau

My apologies, I forgot to show that I had cross-posted, which I usually do

Regards

Howard
 
Upvote 0

Forum statistics

Threads
1,221,537
Messages
6,160,405
Members
451,644
Latest member
hglymph

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