Converting numbers to special symbols and converting the symbols back to numbers using vba

kelly mort

Well-known Member
Joined
Apr 10, 2017
Messages
2,169
Office Version
  1. 2016
Platform
  1. Windows
I am trying to assign symbols to digits as below:

0 = !
1 = @
2 = #
3 = <
4 = ~
5 = :
6 = $
7 = %
8 = ?
9 = &

So that When I have a number like "2031", I want to convert it to:

#! <@

And in the case of ?~$$ I want to convert it to 8466

How do I do that?

Thanks in advance.
Kelly
 
Hi Rick,​
why not an unique function rather than two ? …​
I guess I'm kind of old school... each function does one thing which in turn makes the code a little more self documenting. For a single function using my approaches...
VBA Code:
Function Crypto(S As String) As String
  Dim X As Long
  Crypto = S
  For X = 1 To Len(S)
    If Left(S, 1) Like "#" Then
      Mid(Crypto, X) = Mid("!@#<~:$%?&", Mid(S, X, 1) + 1, 1)
    Else
      Mid(Crypto, X) = InStr("!@#<~:$%?&", Mid(S, X, 1)) - 1
    End If
  Next
End Function
 
Upvote 0

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Okay, as it turns out, the functions I post are also too long. Here are revised functions that, for my individual functions, are one-liners and while not a one-liner, the "unique" function Marc asked about is still quite compact. Here are these functions...
VBA Code:
Function Num2Sym(S As String) As String
  Num2Sym = Join(Evaluate("TRANSPOSE(MID(""!@#<~:$%?&"",MID(""" & S & """,ROW(1:" & Len(S) & "),1)+1,1))"), "")
End Function
VBA Code:
Function Sym2Num(S As String) As String
  Sym2Num = Join(Evaluate("TRANSPOSE(FIND(MID(""" & S & """,ROW(1:" & Len(S) & "),1),""!@#<~:$%?&""))-1"), "")
End Function
VBA Code:
Function Crypto(S As String) As String
  If S Like "#*" Then
    Crypto = Join(Evaluate("TRANSPOSE(MID(""!@#<~:$%?&"",MID(""" & S & """,ROW(1:" & Len(S) & "),1)+1,1))"), "")
  Else
    Crypto = Join(Evaluate("TRANSPOSE(FIND(MID(""" & S & """,ROW(1:" & Len(S) & "),1),""!@#<~:$%?&""))-1"), "")
  End If
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,923
Messages
6,122,286
Members
449,076
Latest member
kenyanscott

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