Unique Account Numbers

JohnStevensInc

Board Regular
Joined
Dec 2, 2003
Messages
109
Is there a way to combine two alphabetical fields and create a unique account number? In other words, if A1 is "Smith, Jim" and A2 is "Great Northern Company" can you create a formula in A3 that will combine the two and create a unique account number that is numerical?

Thanks.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
How would you do this outside Excel ?

You can concatenate the two cells like this
Code:
=a1&" "&a2
but the result isn't numerical.
 
Upvote 0
You want to use a hash table to create a number from each letter and combine that to form a unique number, forming a meaningful ID number.

...

The point of this escapes me. If you want to create a meaningful ID, do what was suggested above. If you want a unique ID, I'd suggest using the row() function. If you want something unique and meaningful, combine the two
Code:
 = $A1 & "-" & $B1 & ":" & row()
However, you can't just convert text into numbers in a single cell, though it is possible by using the ASCII values (the asc command in VBA), but you can't convert text into numbers to form a unique ID period. The most you can do for that is to create a translated number and then add 1 until it's unique.
 
Upvote 0
Try this. Press ALT + F11 to open the Visual Basic Editor, Insert > Module and paste in

Code:
Function ACID(R1 As Range, R2 As Range) As String
Application.Volatile
Dim i As Integer
For i = 1 To Len(R1)
    ACID = ACID & Asc(Mid(R1.Value, i, 1))
Next i
For i = 1 To Len(R2)
    ACID = ACID & Asc(Mid(R2.Value, i, 1))
Next i
End Function

Close the VBE using the X then enter a formula like

=acid(A1,B1)
 
Upvote 0
JohnStevensInc,

Thank you VOG II.

Here is another solution - a sum of the characters in the two strings.

Excel Workbook
A
1Smith, Jim
2Great Northern Company
33019
Sheet1



Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into the Module1.

Code:
Option Explicit
Function SumCODE(c1 As Range, c2 As Range) As Integer
    Dim Total%, LoopCtr%
    For LoopCtr% = 1 To Len(c1) Step 1
        Total% = Total% + Asc(Mid(c1.Value, LoopCtr%, 1))
    Next LoopCtr%
    For LoopCtr% = 1 To Len(c2) Step 1
        Total% = Total% + Asc(Mid(c2.Value, LoopCtr%, 1))
    Next LoopCtr%
    SumCODE = Total%
End Function


Copy the following formula in cell A3:
=SumCODE(A1,A2)


Have a great day,
Stan
 
Upvote 0
Is there any way to convert text into a unique number?
Hi,

Not strongly unique but close to it, at least stronger than the sum of each Asc(char):
Rich (BB code):
Dim crc32Table() As Long

'--------------------------------------------------
' Function  : Crc32
' Parameter : StrBuf
' Purpose   : Calc CRC32 of StrBuf string
' VBA usage : Call Crc32("My test string")
' XL  usage : =Crc32(A1&A2)
'--------------------------------------------------
Function Crc32(StrBuf As String) As Long
  Dim i%, j%
  Crc32 = -1
  On Error Resume Next
  i = UBound(crc32Table)
  If Err <> 0 Then Call InitCrc32
  For i = 1 To Len(StrBuf)
    j = (Crc32 And 255) Xor Asc(Mid$(StrBuf, i, 1))
    Crc32 = ((Crc32 And -256) \ 256) And 16777215
    Crc32 = Crc32 Xor crc32Table(j)
  Next i
  Crc32 = Not (Crc32)
End Function

'--------------------------------------------------
' Procedure : InitCrc32
' Purpose   : Fill crc32Table by PKZip polinomial
'--------------------------------------------------
Private Sub InitCrc32()
  Dim Crc&, i%, j%
  ReDim crc32Table(0 To 255)
  For i = 0 To 255
    Crc = i
    For j = 8 To 1 Step -1
      If (Crc And 1) Then
        Crc = ((Crc And -2) \ 2&) And 2147483647
        Crc = Crc Xor -306674912
      Else
        Crc = ((Crc And -1) \ 2&) And 2147483647
      End If
    Next j
    crc32Table(i) = Crc
  Next i
End Sub

'--------------------------------------------------
' Procedure : Test_Crc32
' Purpose   : Testing
'--------------------------------------------------
Sub Test_Crc32()
  Dim s As String
  s = "First Ladies rule the State and state the rule: ladies first"
  MsgBox s & vbCr & "CRC32=" & Crc32(s)
End Sub
Regards,
Vladimir
 
Last edited:
Upvote 0
Not to trivialize this, but couldn't you just create a unique list of your customers and label them 1, 2, ..., 1000 etc?
 
Upvote 0
Nice Ascii conversion, guys, but as I said earlier, you have to do what windows does, add 1 to the number until it truly is unique.

You could either do a search through the IDs for matches

Code:
dim continue as boolean
dim ID as long ' Your Previously formed ID Number
continue = true
while continue
  for row =topRow to BottomRow
     if datasheet.cells(row, IDCol) = ID then
        ID=ID+1
        row = bottomrow+1
        continue
     endif
  next
wend

or do circular logic in the Excel sheet itself (requires iteration enabled)
Code:
datasheet.cells(row,IDCol).formula = "= if(R1C:R[-1]C = RC, RC+1,RC)"
datasheet.cells(row,IDCol).value = ID

The VBA unique number is more stable, but the second version is more fun.
However, in the end, there is really no point to doing this as the number isn't meaningful and can't be back-translated to the data since the data itself won't be unique. You would be just as well off using a random number generator or the row() function.
 
Upvote 0

Forum statistics

Threads
1,221,487
Messages
6,160,117
Members
451,619
Latest member
KunalGandhi

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