Substitution of letters

elle_clara

New Member
Joined
Jul 28, 2013
Messages
11
Hi, I've been trying to make a macro for making a simple substitution cypher but although I managed to do so in Python I don't know enough to make a macro.

The desired outcome would be to type a phrase in, say, cell A1, run the macro and then have the result appear in cell A2.

It's not a simple offset by a given number of characters (like a->c, b->d, etc) but rather a substitution that I'd write for each and every letter on a different row, e.g:
a->s
b->a
c->t

A1: abc -> A2: sat

I guess I'd need to use the Replace function but I'm not sure how to make it write in cell A2 the substituted form.
 
Last edited:
For example..

Code:
Dim x As String
x = ChrW(&H15F)
sFm = "ABC & X"



I mean, something like this; where you could enter your Unicode characters directly in the cells in "To" column on the worksheet.

Excel 2013
IJ
FromTo
az
bx
cc
dv
eb
fn
gm
ha
is
jd
kf
lg
mh
nj
ok
pl
qq
rw
se
tr
ut
vy
wu
xi
yo
zp

<colgroup><col style="width: 25pxpx"><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]

[TD="align: center"]2[/TD]

[TD="align: center"]3[/TD]

[TD="align: center"]4[/TD]

[TD="align: center"]5[/TD]

[TD="align: center"]6[/TD]

[TD="align: center"]7[/TD]

[TD="align: center"]8[/TD]

[TD="align: center"]9[/TD]

[TD="align: center"]10[/TD]

[TD="align: center"]11[/TD]

[TD="align: center"]12[/TD]

[TD="align: center"]13[/TD]

[TD="align: center"]14[/TD]

[TD="align: center"]15[/TD]

[TD="align: center"]16[/TD]

[TD="align: center"]17[/TD]

[TD="align: center"]18[/TD]

[TD="align: center"]19[/TD]

[TD="align: center"]20[/TD]

[TD="align: center"]21[/TD]

[TD="align: center"]22[/TD]

[TD="align: center"]23[/TD]

[TD="align: center"]24[/TD]

[TD="align: center"]25[/TD]

[TD="align: center"]26[/TD]

[TD="align: center"]27[/TD]

</tbody>
Sheet1

I tried using
Code:
Dim x As String
x = ChrW(&H15F)
and replacing in the sFm and sTo but it doesn't work.

That replacement table would be perfect! How should I write the instructions? I suppose it will be 28 rows (not that I mind) of the type I1 → J1. That "→" there is my problem :P no idea how to do it. It would be awesome to keep it as a function with text entered in A1 and appearing in the coded form in A2.

Also thanks for bearing with me :D
 
Upvote 0

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Hi, try like this:

Code:
Function cypher(s As String, rFrom As Range, rTo As Range, Optional Decode As Boolean) As String
Dim sFm As String, sTo As String
sFm = Join(Application.Transpose(rFrom), "")
sTo = Join(Application.Transpose(rTo), "")
Dim i As Long, n As Long
For i = 1 To Len(s)
    n = InStr(1, IIf(Decode, sTo, sFm), Mid(s, i, 1), vbTextCompare)
    If n Then
        cypher = cypher & Mid(IIf(Decode, sFm, sTo), n, 1)
            Else
        cypher = cypher & Mid(s, i, 1)
    End If
Next i
End Function

Note: cell F6 contains the "" character; but the MrExcelHTML add-in can't display Unicode characters so it will appear as a "?"


Excel 2013
ABCDEF
1hello worlds?ool dliowhello worldFromTo
2mr excelni ?cx?omr excelaz
3formruliniformrby
4elle_cara?oo?_xzizelle_caracx
5dw
6e?
7fu
8gt
9hs
10ir
11jq
12kp
13lo
14mn
15nm
16ol
17pk
18qj
19ri
20sh
21tg
22uf
23ve
24wd
25xc
26yb
27za
Sheet1
Cell Formulas
RangeFormula
B1=cypher(A1,$E$2:$E$27,$F$2:$F$27)
C1=cypher(B1,$E$2:$E$27,$F$2:$F$27,TRUE)
F6=UNICHAR(8594)
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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