Search For Double Characters and Place Character In-Between

Manfred

New Member
Joined
Jan 11, 2014
Messages
10
Is there a simple way to find all double characters in a string (of variable length) and place another character in-between them? I'm completely stumped!

eg. "MESSAGE" would become "MESXSAGE"
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this for String in "A1"
Code:
[COLOR="Navy"]Sub[/COLOR] MG01Dec34
[COLOR="Navy"]Dim[/COLOR] n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long,[/COLOR] Temp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]For[/COLOR] n = 1 To Len([a1])
    [COLOR="Navy"]If[/COLOR] Not IsEmpty(Temp) [COLOR="Navy"]Then[/COLOR]
        [COLOR="Navy"]If[/COLOR] Mid([a1], n, 1) = Temp [COLOR="Navy"]Then[/COLOR]
            [a1] = Mid([a1], 1, n - 1) & "X" & Mid([a1], n)
        [COLOR="Navy"]End[/COLOR] If
    [COLOR="Navy"]End[/COLOR] If
Temp = Mid([a1], n, 1)
[COLOR="Navy"]Next[/COLOR] n
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
A couple of comments.

Mick, you may want to revisit that code considering A1 might hold "Mississippi" or "Wooloomooloo" (Suburb of Sydney BTW :))

Manfred, could the string hold a name (or any word) like "Llewellyn", and if so should it have the "X" between those first two Ls as well as the second two? I ask because Mick's code would also need a tweak for that.

An alternative suggestion. Change the IgnoreCase line to False if you do want the check to be case-sensitive.

Rich (BB code):
Sub SplitDouble()
  With CreateObject("vbScript.RegExp")
    .Global = True
    .IgnoreCase = True
    .Pattern = "(.)\1"
    Range("A1").Value = .Replace(Range("A1").Value, "$1X$1")
  End With
End Sub
 
Upvote 0
Hi

Also are these really names or codes.

If they are codes there can be a sequence of 3, 4 equal characters. In that case what would you like, insert the X after each character? after each 2?

Ex.
AAAA becomes AXAAXA or AXAXAX?
 
Upvote 0
I had wondered about PGCs question too but guessed the strings were normal words.

My previous code would turn "AAAA" into "AXAAXA"

If it should be "AXAXAXA" then just change these two lines of my code.
Rich (BB code):
.Pattern = "(.)(?=\1)"
Range("A1").Value = .Replace(Range("A1").Value, "$1X")
 
Upvote 0
Good points. They are indeed words, however, I had not taken into consideration the fact I need to trim all of the spaces out of the string, thus potentially creating more doubles. Peter, your code takes care of that nicely!

Thanks all
 
Upvote 0
Glad you got something suitable. :)

For interest, if you (or anyone reading) wanted a non-RegExp solution, then this UDF that could be used in a worksheet as shown below or just within vba might be worth a try.
It gives additional options of what character(s) to place between the repeated characters and whether to check in a case-sensitive manner or not.

Rich (BB code):
Function SplitDoubles(s As String, Delim As String, Optional CheckCase As Boolean = False) As String
  Dim i As Long
  Dim t As String
  
  t = s
  If Not CheckCase Then t = LCase(s)
  For i = Len(t) To 2 Step -1
    If Mid(t, i, 1) = Mid(t, i - 1, 1) Then
      s = Left(s, i - 1) & Delim & Mid(s, i)
    End If
  Next i
  SplitDoubles = s
End Function

Excel Workbook
ABCD
1MESSAGEMESXSAGEMES SAGEMES:-:SAGE
2CatCatCatCat
3MississippiMisXsisXsipXpiMis sis sip piMis:-:sis:-:sip:-:pi
4AaAaAAXaXAXaXAAaAaAA:-:a:-:A:-:a:-:A
5LlewellynLXlewelXlynLlewel lynL:-:lewel:-:lyn
Sheet2
 
Upvote 0
I've just tried that out - really nice! I'm still learning at the moment, I'm quite a long way off being able to write my own UDFs, but hopefully I'll get there one day!
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,285
Members
449,218
Latest member
Excel Master

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