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:

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi, here is a UDF that you could try:

Code:
Function cypher(s As String, Optional Decode As Boolean) As String
Const sFm = "abcdefghijklmnopqrstuvwxyz"
Const sTo = "zxcvbnmasdfghjklqwertyuiop"
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

Which could be used in the worksheet like this, for example:


Excel 2013
ABC
1hello worldabggk ukwgvhello world
2mr excelhw bicbgmr excel
3FormRnkwhwformr
4elle_clarabggb_cgzwzelle_clara
Sheet1
Cell Formulas
RangeFormula
B1=cypher(A1)
C1=cypher(B1,TRUE)


Or called via code like this for example:

Code:
Sub EncodeIt()
Range("B1").Value = cypher(Range("A1").Value)
End Sub


Sub DecodeIt()
Range("C1").Value = cypher(Range("B1").Value, True)
End Sub
 
Upvote 0
Something like this:

Code:
Cells(1, 2).Value = replace(Cells(1, 2).Value, "old", "new")

Hey thanks, I don't really know how cells(x,y) works and I get an error. I tried with Range("A2").Value=replace(Range("A1").Value, "a", "s") and it works, but only sort of. It works only for the most recent row. So in my example it just replaces "c"→"t".

Hi, here is a UDF that you could try:

It's awesome! It works really well, I love the fact that I can use it as a function just like any other in excel.
One last question: I know that if I want to use a non-ASCII character I should use ChrW(&H1234) where 1234 is the unicode hex code. But I'm not sure how to insert it in the letter strings sFm and sTo.
I don't mind writing the unicode hex codes for each and every character if there's a way to make it work with the current way it makes the replacements. But if a whole new approach is needed don't worry, it's really good as it is even now!
 
Last edited:
Upvote 0
Hi, not tested as just about to pop out for a while, but you could try something like this:

Code:
Function cypher(s As String, Optional Decode As Boolean) As String
Dim sFm As String
Dim sTo As String


sFm = "abcdefghijklmnopqrstuvwxyz" & ChrW(&H1234) & ChrW(&H4567)
sTo = "zxcvbnmasdfghjklqwertyuiop" & ChrW(&H4567) & ChrW(&H1234)


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
 
Upvote 0
Hi, not tested as just about to pop out for a while, but you could try something like this:

Ugh it gives the error "constant expression required". I tried to define a constant x as
Const x = ChrW(&H15F)
and use it in the sFm as [...] & x, but the same error appears.
I probably don't understand how constants work in Excel. But don't worry it's really good this way already!

@jwalrath yes, it works if you want to replace a letter by a digit.
 
Last edited:
Upvote 0
Hi, you would need to define x as a string and then assign it's value.

It sounds like it might be easier for you to define the "To" and "From" characters in a range on the worksheet - it wouldn't be a big deal to change the UDF if that method would be preferable?
 
Upvote 0
Hi, you would need to define x as a string and then assign it's value.

It sounds like it might be easier for you to define the "To" and "From" characters in a range on the worksheet - it wouldn't be a big deal to change the UDF if that method would be preferable?

How could I define it as a string?
Code:
Const x = ChrW(&H15F) as String
I'm sorry, I'm really a noobie in VBA.

As for characters defined in a range, do you mean like bellow?
Code:
A1: abcde [...]
A2: hskrz [...]
If so, yes that would be really good! Can you tell me how should I alter the function, pretty please?
 
Last edited:
Upvote 0
How could I define it as a string?

For example..

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

As for characters defined in a range, do you mean like bellow?

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
1FromTo
2az
3bx
4cc
5dv
6eb
7fn
8gm
9ha
10is
11jd
12kf
13lg
14mh
15nj
16ok
17pl
18qq
19rw
20se
21tr
22ut
23vy
24wu
25xi
26yo
27zp
Sheet1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

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