+ Reply to Thread
Results 1 to 14 of 14

Removing repeated text across several cells (same column) and keeping first instance

Hybrid View

  1. #1
    Registered User
    Join Date
    11-23-2022
    Location
    Sao Paulo
    MS-Off Ver
    Excel for Mac
    Posts
    15

    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.

    Thank you so much, as usual.
    Attached Files Attached Files

  2. #2
    Forum Expert nankw83's Avatar
    Join Date
    08-31-2015
    Location
    Kuwait
    MS-Off Ver
    365
    Posts
    1,712

    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

  3. #3
    Registered User
    Join Date
    11-23-2022
    Location
    Sao Paulo
    MS-Off Ver
    Excel for Mac
    Posts
    15

    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?

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool 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 
    "¤"
            
    Dim VR&, S&, C%, T$
        
    With [B3].CurrentRegion
            V 
    = .Value
        With 
    New Collection
            On Error Resume Next
        
    For 2 To UBound(V)
            
    V(R1) = Split(V(R1), vbLf)
        For 
    0 To UBound(V(R1))
            
    0
            T 
    Trim(Split(V(R1)(S), "-")(1))
            
    = .Item(T)
            If 
    C Then V(R1)(S) = Else .Add 1T
        Next
            V
    (R1) = Join(Filter(V(R1), DFalse), 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 ...

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Removing repeated text across several cells (same column) and keeping first instance

    Hi adribas,

    If you had the latest version of Excel 365 you could use the newer =TextJoin and =TextSplit functions and do your problem with this formula:
    Formula: copy to clipboard
    =UNIQUE(TEXTSPLIT(TEXTJOIN(CHAR(10),,$B$4:$B$7),,CHAR(10),TRUE))

    See the attached..
    sample filtering repeated words answer.xlsx
    By the way you have an extra space in - orange is a fruit so it doesn't match -orange is a fruit.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  6. #6
    Registered User
    Join Date
    11-23-2022
    Location
    Sao Paulo
    MS-Off Ver
    Excel for Mac
    Posts
    15

    Re: Removing repeated text across several cells (same column) and keeping first instance

    Hi Marvin, I tried that but it returns #CALC! probably because there's loads of text to be filtered through.

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,623

    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
    Last edited by jindon; 11-26-2022 at 04:33 AM.

  8. #8
    Registered User
    Join Date
    11-23-2022
    Location
    Sao Paulo
    MS-Off Ver
    Excel for Mac
    Posts
    15

    Re: Removing repeated text across several cells (same column) and keeping first instance

    Hi jindon, this is what I got when I ran your script
    Run-time error '7':

    Out of memory

  9. #9
    Registered User
    Join Date
    11-23-2022
    Location
    Sao Paulo
    MS-Off Ver
    Excel for Mac
    Posts
    15

    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.
    Attached Files Attached Files
    Last edited by adribas; 11-26-2022 at 05:22 AM.

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Removing repeated text across several cells (same column) and keeping first instance


    Quote Originally Posted by adribas View Post
    Hi all, I'm attaching the original file here.
    Such a bad initial post attachment !

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,772

    Re: Removing repeated text across several cells (same column) and keeping first instance

    Quote Originally Posted by Marc L View Post
    Such a bad initial post attachment !
    This comment is not constructive. Either provide specific areas of improvement or don't comment.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,623

    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

  13. #13
    Forum Guru HansDouwe's Avatar
    Join Date
    06-21-2022
    Location
    Nederland
    MS-Off Ver
    365 V2403 (Build 17330.20000)
    Posts
    6,465

    Re: Removing repeated text across several cells (same column) and keeping first instance

    In Excel 365 is this also possible with a formula:

    Formula: copy to clipboard
    =LET(i,TRIM(TEXTSPLIT(B4,"-",,1)),t,REDUCE("",B$3:B3,LAMBDA(a,b,HSTACK(a,TRIM(TEXTSPLIT(b,"-",,1))))),f,"- "&TEXTJOIN("- ",,IFERROR(FILTER(i,ISNA(MATCH(i,t;0))),"")),f)
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    11-23-2022
    Location
    Sao Paulo
    MS-Off Ver
    Excel for Mac
    Posts
    15

    Re: Removing repeated text across several cells (same column) and keeping first instance

    Thank you so much everyone for your help! Guru jindon solved my issue successfully.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Remove Duplicate - Specific instance but keeping data from removed instance?
    By NewYears1978 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 04-22-2022, 12:59 PM
  2. [SOLVED] Removing duplicates from column A while keeping their respective values from column B
    By joel.mugabe in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 02-19-2016, 02:38 AM
  3. Finding the last instance of a text in a column and deleting all cells with values af
    By devirecui in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2015, 07:31 AM
  4. Removing Merged cells keeping entries order
    By Odracir in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-29-2015, 02:21 AM
  5. Formula for counting text values in column when value is repeated in cells
    By Blubirdjim in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 12-11-2012, 12:36 PM
  6. Removing Formulas keeping Values as Text
    By dsexpress in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-09-2009, 10:45 AM
  7. Removing all text from a column of cells
    By jimbob in forum Excel General
    Replies: 3
    Last Post: 02-07-2009, 05:14 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1