Coverting Alphabets into Numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I hope someone can help me with this.

I have a column of "encrypted" data (in excel), which needs to be converted.
The data in each cell is "encrypted" by following this simple rule:

A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
J =0

Therefore, if a cell has "BFDJ", the numerical value will be 2640.

My problem is this:

How can I take the sum of the "encrypted" data in the column to make a grand
total, which is also encrypted using the above rule?

I've looked around and it seems that there are functions which can change
numbers into words, but not the other way round.

Any assistance is much appreciated.
 
-Paste this code into a module
-Type, "BFDJ", into cell "A1"
-Type, =Decode("A1"), in cell "A2"
-Cell "A2" will return 2640

Regards,
Dave


Public Function DeCode(r As Variant) As Long
Dim i As Integer, _
l As String

For i = 1 To Len(r)
l = l & GetValue(Mid(r, i, 1))
Next

DeCode = l
End Function

Private Function GetValue(s As String) As String
Select Case s
Case Is < "J"
GetValue = Asc(s) - 64
Case "J"
GetValue = "0"
End Select
End Function
 
Try this function. It will give you the decoded total for any range of cells
you choose.

=decodenum(A1:B5)

Function decodenum(Target As Range) As Integer

Total = 0

For Each cell In Target


Subtotal = 0
For Count = 1 To Len(cell)
Subtotal = (10 * Subtotal) + _
((Asc(Mid(cell, Count, 1)) - Asc("A") + 1) Mod 10)

Next Count

Total = Total + Subtotal
Next cell
decodenum = Total
End Function
 
Marcus,
Not sure if this wil prove faster, but avoiding the Mid/Left etc text
function in favour of maths (and 1 concatenation) is an alternative:

Public Function DecodeString(EncodedStr As String) As Long
Dim bytArray() As Byte
Dim i As Long
Dim OutStr As String

Const ASCII_OFFSET As Long = 64
Const MAX_CODE As Long = 10

bytArray = UCase(EncodedStr)

For i = LBound(bytArray) To UBound(bytArray) Step 2
bytArray(i) = bytArray(i) - ASCII_OFFSET
If bytArray(i) >= MAX_CODE Then bytArray(i) = 0
OutStr = OutStr & bytArray(i)
Next

DecodeString = CLng(OutStr)

End Function

NickHK
 
Thank you all. From the examples, I was also able to figure out how to
"encrypt" it back to alphabets.

Thanks!
 
Hello Marcus,

Just for the fun of it: If you like to decrypt via worksheet
functions, I suggest to use
=SUMPRODUCT(MOD(CODE(MID($A$1,ROW(INDIRECT("1:"&LEN($A$1))),1))-64,10),
10^(LEN($A$1)-ROW(INDIRECT("1:"&LEN($A$1)))))

Regards,
Bernd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top