+ Reply to Thread
Results 1 to 7 of 7

Deleting duplicated data from 1 cell.

Hybrid View

  1. #1
    Registered User
    Join Date
    01-22-2010
    Location
    Tredegar, South Wales
    MS-Off Ver
    Excel 2007
    Posts
    1

    Deleting duplicated data from 1 cell.

    Hi,
    I am a new user to this site and also new user of excel. I need help to search individual cells that contains multiple strings of data, and then delete the duplicated data contained in that cell. Is this possible?

    Many Thanks

    Ben

    This below shows what data would be contained in 1 cell.

    CN2233, CN2233, CN2134, CN2266, CN2233, CN2136, CN2266

    And this would ideally be the result after the process/formula/macro

    CN2233, CN2134, CN2266, CN2136

    so the duplicated data would be deleted.

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Deleting duplicated data from 1 cell.

    Is it essential that you keep this data in the one cell? Is it possible to parse the data into multiple cells? of course everything is possible, but let's first ensure that what you are asking for is what you actually need.

  3. #3
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Deleting duplicated data from 1 cell.

    try..
    Option Explicit
    Sub ptest()
        Dim Unqiue As New Collection, UnqiueVal, a, x
        For Each UnqiueVal In Split(Range("A3"), ",")
            Unqiue.Add UnqiueVal, CStr(UnqiueVal)
            On Error Resume Next
        Next UnqiueVal
        For a = 1 To Unqiue.Count
            Debug.Print Unqiue.Item(a)
            x = Unqiue.Item(a) + "," + x
        Next
        Range("B3") = x
    End Sub
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  4. #4
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Deleting duplicated data from 1 cell.

    this is better it gets ride of the trailing ,
    Option Explicit
    Sub ptesttwo()
        Dim Unqiue As New Collection, UnqiueVal, a, x
        For Each UnqiueVal In Split(Range("A3"), ",")
            y = Trim(UnqiueVal)
            Unqiue.Add UnqiueVal, CStr(y)
            On Error Resume Next
        Next UnqiueVal
        For a = 1 To Unqiue.Count
             x = x & IIf(x <> "", ",", "") & Unqiue.Item(a)
        Next
        Range("A3").ClearContents
        Range("A3") = x
    End Sub
    Last edited by pike; 01-22-2010 at 10:31 PM. Reason: add clearcontents

  5. #5
    Forum Expert
    Join Date
    10-10-2008
    Location
    Northeast Pennsylvania, USA
    MS-Off Ver
    Excel 2007
    Posts
    2,387

    Re: Deleting duplicated data from 1 cell.

    Ben Lewis,

    See the attached workbook "Array - Deleting duplicate data from a cell - Ben Lewis - SDGD12.xlsm" with macro "RemoveStringDups".

    Detach the workbook and run the "RemoveStringDups" macro.
    Have a great day,
    Stan

    Windows 10, Excel 2007, on a PC.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved by clicking EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

  6. #6
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Deleting duplicated data from 1 cell.

    hi all,

    Here's another variant which is similar to Pike's & Stanley's - I tried to post last night but had connection issues.
    It works on the current selection (no error checking for selection type though) & uses Join after working through the elements of the array. I have tried to make it work for non-contiguous ranges as well by looping through the selected areas. It is based on some of Jindon's code that I picked up some time ago.
    Potentially, it (or the others) could be made faster by creating in memory arrays based on the range, processing the strings, & then writing them back to the sheet in one hit (instead of one cell at a time). This would become more significant as the selection size increases.

    Option Explicit
    Public glb_origCalculationMode As Long
    'an added extra, in case there are formulae which depend on the cells in the processed range.
    Sub ToggleRefreshApp(RefreshAppSettings As Boolean)
        With Application
            If Not RefreshAppSettings Then
                glb_origCalculationMode = .Calculation
            End If
            .EnableEvents = RefreshAppSettings
            On Error Resume Next
            .Calculation = IIf(RefreshAppSettings, glb_origCalculationMode, xlCalculationManual)
            '.Calculation = IIf(RefreshAppSettings, xlCalculationAutomatic, xlCalculationManual)
            On Error GoTo 0
            .ScreenUpdating = RefreshAppSettings
            .StatusBar = False    'this should really be stored as a glb variable & restored, but impact is likely to be minimal
        End With
    End Sub
    
    Sub RemoveDupsFromWithinCellStr()
    Const DelimiterStr As String = ", "
    Dim ArryDir As Variant
    Dim i As Long
    Dim j As Long
    Dim nodupes As New Collection
    Dim cll As Range
    Dim Swap1 As Variant
    Dim Swap2 As Variant
    Dim nwArr As Variant
    Dim trgt As Range
    Dim CllArea As Range
        Call ToggleRefreshApp(False)
        Set trgt = Selection
        For Each CllArea In trgt
            For Each cll In CllArea
                ArryDir = Split(cll.Value2, DelimiterStr, -1)        'minus 1 is Optional (added by me)
                For i = LBound(ArryDir) To UBound(ArryDir)
                    On Error Resume Next
                    If Trim(ArryDir(i)) <> "" Then nodupes.Add Trim(ArryDir(i)), Trim(ArryDir(i))
                    '          Note: the 2nd argument (key) for the Add method must be a string
                    '      Resume normal error handling
                    On Error GoTo 0
                Next i
                '            '   Sort the collection (optional)
                '            For i = 1 To nodupes.Count - 1
                '                For j = i + 1 To nodupes.Count
                '                    If nodupes(i) > nodupes(j) Then
                '                        Swap1 = nodupes(i)
                '                        Swap2 = nodupes(j)
                '                        nodupes.Add Swap1, Before:=j
                '                        nodupes.Add Swap2, Before:=i
                '                        nodupes.Remove i + 1
                '                        nodupes.Remove j + 1
                '                    End If
                '                Next j
                '            Next i
                '   Delete existing items in list, add the sorted (?), non-duplicated items back to the cell
                ReDim nwArr(0 To nodupes.Count - 1)
                For i = 0 To nodupes.Count - 1
                    nwArr(i) = nodupes.Item(i + 1)
                Next i
                'for testing...            If Not IsEmpty(nwArr) Then cll.Offset(0, 1).Value2 = Join(nwArr, DelimiterStr)
                If Not IsEmpty(nwArr) Then cll.Value2 = Join(nwArr, DelimiterStr)
            Next cll
        Next CllArea
        Call ToggleRefreshApp(True)
    End Sub

    hth
    Rob
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,330

    Re: Deleting duplicated data from 1 cell.

    hi Ben Lewis
    or with the more adaptable dictionary
    Option Explicit
    Sub ptesttwo()
        Dim y$, x, z
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For Each z In Split(Range("A3"), ",")
                y = Trim(z)
                If Not .exists(y) Then
                    .Add y, y
                    x = x & IIf(x <> "", ",", "") & y
                End If
            Next
            Range("A3").ClearContents
            Range("A3") = x
        End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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