Remove adjacent duplicate characters from String A and remove characters in the same position from String B.

alecambo

New Member
Joined
Apr 10, 2015
Messages
35
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I have two strings of letters (columns String A and String B) and I want to transform them into strings like the ones in Solution 1 and Solution 2 correspondingly.
In the case of String A, I need to remove any adjacent duplicate characters, so the string ZPDUU would turn into ZPDU.
String B is a little bit more complicated: both String A and String B have the same length; and I want to transform String 2 depending on the transformation of String 1 into Solution 1. For example, in my data, I have ZPDUU as String 1 and OOBOO as String 2. Solution 1 would be ZPDU (remove duplicate characters) and Solution 2 would be OOBO (remove characters that are in the same position as the characters removed from String 1).
I hope this makes sense and I'm happy to provide any further clarification.

String AString BSolution 1Solution 2
ZPDUU
OOBOO
ZPDUOOBO
OJGGDUU
UOOOBOO
OJGDUUOOBO
ZFDD
UOBB
ZFDUOB
JJJIIKSHHWQTTT
UUUUUUUUUOPOOO
JIKSHWQT
UUUUUOPO

Thanks in advance,
alecambo
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Run this macro:

VBA Code:
Sub removeduplicate()
  Dim i As Long, j As Long
  Dim a As Variant
  Dim dic As Object
  Dim cad As String
  
  Set dic = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("B" & Rows.Count).End(3)).Value
  ReDim b(1 To UBound(a, 1), 1 To 2)
  
  For i = 1 To UBound(a, 1)
    dic.RemoveAll
    For j = 1 To Len(a(i, 1))
      cad = Mid(a(i, 1), j, 1)
      If Not dic.exists(cad) Then
        dic(cad) = Empty
        b(i, 1) = b(i, 1) & cad
        b(i, 2) = b(i, 2) & Mid(a(i, 2), j, 1)
      End If
    Next
  Next
  Range("C2").Resize(UBound(b, 1), 2).Value = b
End Sub
 
Upvote 0
A formula option
Fluff.xlsm
ABCD
1String AString BSolution 1Solution 2
2ZPDUUOOBOOZPDUOOBO
3OJGGDUUUOOOBOOOJGDUUOOBO
4ZFDDUOBBZFDUOB
5JJJIIKSHHWQTTTUUUUUUUUUOPOOOJIKSHWQTUUUUUOPO
Input
Cell Formulas
RangeFormula
C2:C5C2=LET(s,SEQUENCE(LEN(A2)-1),CONCAT(IF(MID(A2,s,1)<>MID(A2,s+1,1),MID(A2,s,1),""),RIGHT(A2)))
D2:D5D2=LET(s,SEQUENCE(LEN(A2)-1),CONCAT(IF(MID(A2,s,1)<>MID(A2,s+1,1),MID(B2,s,1),""),RIGHT(B2)))
 
Upvote 0
A slightly simpler version.
Fluff.xlsm
ABCD
1String AString BSolution 1Solution 2
2ZPDUUOOBOOZPDUOOBO
3OJGGDUUUOOOBOOOJGDUUOOBO
4ZFDDUOBBZFDUOB
5JJJIIKSHHWQTTTUUUUUUUUUOPOOOJIKSHWQTUUUUUOPO
Input
Cell Formulas
RangeFormula
C2:C5C2=LET(s,SEQUENCE(LEN(A2)),CONCAT(IF(MID(A2,s,1)<>MID(A2,s+1,1),MID(A2,s,1),"")))
D2:D5D2=LET(s,SEQUENCE(LEN(A2)),CONCAT(IF(MID(A2,s,1)<>MID(A2,s+1,1),MID(B2,s,1),"")))
 
Upvote 0
Solution
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,731
Messages
6,126,537
Members
449,316
Latest member
sravya

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