Removing repeated text across several cells (same column) and keeping first instance
Hello helpful people!
I've done a few searches on the internet and in this forum as well, however I found no response for my issue in specific.
What I'm trying to do is to run a search across individual columns for repeated text, keep the first instance and delete the text from all other cells in the colum with the same value.
I've attached a spreadsheet with an example of what I'm trying to achieve. Delimiter is a carriage return for each sentence.
By all the testing I've done, this needs to be done via VBA, not formula but I might be wrong.
Re: Removing repeated text across several cells (same column) and keeping first instance
Hi adribas,
Try below code ...
Sub test()
Dim a
a = Range("B4", Range("B" & Rows.Count).End(3))
ReDim b(1 To UBound(a), 1 To 1)
With CreateObject("scripting.dictionary")
For x = 1 To UBound(a)
For y = 0 To UBound(Split(a(x, 1), vbLf))
j$ = Split(a(x, 1), vbLf)(y)
If Not .exists(j) Then
.Add j, Nothing
b(x, 1) = IIf(Len(b(x, 1)), b(x, 1) & vbLf & j, j)
End If
Next
Next
End With
[E4].Resize(UBound(b)) = b
End Sub
If I was able to help, you can thank me by clicking the * Add Reputation under my user name
Re: Removing repeated text across several cells (same column) and keeping first instance
Hi nankw83, I tried to run the code you provided but I'm using a Mac and got a weird error about Activex.
When I tried to run it on my windows 10, I goot a 1004 error message.
Any ideas?
Re: Removing repeated text across several cells (same column) and keeping first instance
Hello,
as the delimiter can't be a carriage return !
Anyway according to the attachment with the correct Excel delimiter a VBA basics demonstration for starters :
PHP Code:
Sub Demo1() Const D = "¤" Dim V, R&, S&, C%, T$ With [B3].CurrentRegion V = .Value With New Collection On Error Resume Next For R = 2 To UBound(V) V(R, 1) = Split(V(R, 1), vbLf) For S = 0 To UBound(V(R, 1)) C = 0 T = Trim(Split(V(R, 1)(S), "-")(1)) C = .Item(T) If C Then V(R, 1)(S) = D Else .Add 1, T Next V(R, 1) = Join(Filter(V(R, 1), D, False), vbLf) Next R On Error GoTo 0 End With .Value = V End With End Sub
► Do you like it ? ► ► So thanks to click on bottom left star icon « ★ Add Reputation » ! ◄ ◄
Last edited by Marc L; 11-26-2022 at 01:12 AM.
Reason: typo ...
Re: Removing repeated text across several cells (same column) and keeping first instance
try
Sub test()
Dim a, e, x, i As Long, ii As Long, s As String, myList
ReDim myList(1 To 10000)
With [b3].CurrentRegion
a = .Value
For i = 2 To UBound(a, 1)
x = Split(a(i, 1), vbLf)
a(i, 1) = ""
For Each e In x
s = UCase$(Trim$(Mid$(Trim$(e), 2)))
For ii = 1 To UBound(myList)
If myList(ii) = s Then Exit For
If myList(ii) = "" Then
a(i, 1) = a(i, 1) & IIf(a(i, 1) <> "", vbLf, "") & e
myList(ii) = s: Exit For
End If
Next
Next
Next
.Value = a
End With
End Sub
Re: Removing repeated text across several cells (same column) and keeping first instance
Hi all, I'm attaching the original file here. IT's not sensitive info and can be freely found on the internet. Just so the request is clear, I need a script that goes through each column, reads each sentence within a cell, and look for duplicates within the column and remove the duplicates, keeping only the first instance.
Re: Removing repeated text across several cells (same column) and keeping first instance
change to
Sub test()
Dim a, e, x, i As Long, ii As Long, iii As Long, s As String, myList
ReDim myList(1 To 10000)
With [b4].CurrentRegion
a = .Value
For ii = 1 To UBound(a, 2)
For i = 1 To UBound(a, 1)
If Not IsError(a(i, ii)) Then
x = Split(a(i, ii), vbLf): a(i, ii) = ""
For Each e In x
s = UCase$(Trim$(Mid$(Trim$(e), 2)))
For iii = 1 To UBound(myList)
If myList(iii) = s Then Exit For
If myList(iii) = "" Then
a(i, ii) = a(i, ii) & IIf(a(i, ii) <> "", vbLf, "") & e
myList(iii) = s: Exit For
End If
Next
Next
End If
Next
Next
.Value = a
End With
End Sub
Bookmarks