Changing letters in a text string

scbody

New Member
Joined
Oct 1, 2009
Messages
4
Hi

I have text strings consisting of about 500 letters consisting of A, C, G and T. I need to switch A-->T, C-->G, G-->C and T-->A across the whole string. I've tried searching for methods and several techniques without success.
I bet there is a simple technique, but I don't have it!
Can anyone help?

Many thanks
Simon
 
I'm always happy to be proven wrong (actually, that's a complete lie, but you know what I mean)

Haha, nice!

...Don't be fooled by my post count - I still don't know what a dictionary even is; going to learn something from looking at your code, probably tomorrow.

Tai
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Greetings,

Probably not as slick as it could be, but maybe use a RegExp.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> callit()<br>    Sheet3.Range("A1").Value = SwapLetters(Sheet3.Range("A1"))<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <br><SPAN style="color:#00007F">Function</SPAN> SwapLetters(r <SPAN style="color:#00007F">As</SPAN> Range) <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> REX <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> s <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> REX = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">With</SPAN> REX<br>        .Global = <SPAN style="color:#00007F">True</SPAN><br>        .Pattern = "A"<br>        s = .Replace(r.Value, Chr(162))<br>        .Pattern = "C"<br>        s = .Replace(s, Chr(163))<br>        .Pattern = "G"<br>        s = .Replace(s, Chr(164))<br>        .Pattern = "T"<br>        s = .Replace(s, Chr(165))<br>        <br>        .Pattern = Chr(162)<br>        s = .Replace(s, "T")<br>        .Pattern = Chr(163)<br>        s = .Replace(s, "G")<br>        .Pattern = Chr(164)<br>        s = .Replace(s, "C")<br>        .Pattern = Chr(165)<br>        s = .Replace(s, "A")<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <br>    SwapLetters = s<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Function</SPAN></FONT>

If I was going thru oodles of cells, I might create the object in the calling sub.

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,215,873
Messages
6,127,451
Members
449,383
Latest member
DonnaRisso

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