Solved

Remove duplicates form the same row and other rows

Posted on 2014-10-23
4
108 Views
Last Modified: 2014-10-24
can an expert provide the answer to this in the form of VBA code?

I need to remove all duplicates from all rows

so I could have 10, 20 30 rows of

AIOF,AIOF,AIOF,AIOF,AIOFPLC,AIOFPLC,AMHH,AMHH,APFT,APFT,BUNP,BUNP,BUNP,BUNP,EGTO,EGTO,EGTO,EGTO
AIOF,AIOF,AIOF,AIOF,AIOFPLC,AIOFPLC,AMHH,AMHH,APFT,APFT,BUNP,BUNP,BUNP,BUNP,EGTO,EGTO,EGTO,EGTO
AIOF,AIOF,AIOF,AIOF,AIOFPLC,AIOFPLC,AMHH,AMHH,APFT,APFT,BUNP,BUNP,BUNP,BUNP,EGTO,EGTO,EGTO,EGTO

and I only want one occurance of each , so if I have 3 rows like the above I want to end up with in row like the below. [this can be put on a new worksheet]

AIOF,,AIOFPLC,AMHH,APFT,APFT,BUNP,EGTO,


thanks
0
Comment
Question by:Jagwarman
4 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 40399643
And what should happen with

AIOF,AIOF,AIOF,AIOF,AIOFPLC,AIOFPLC,AMHH,AMHH,APFT,APFT,BUNP,BUNP,BUNP,BUNP,EGTO,EGTO,EGTO,EGTO
AIOF,AIOF,AIOFPLC,AMHH,AMHH,APFT,APFT,BUNP,BUNP,EGTO,EGTO

are those also duplicates?
And can the original worksheet be modified (i.e. duplicates removed here)?
0
 
LVL 25

Assisted Solution

by:ProfessorJimJam
ProfessorJimJam earned 250 total points
ID: 40399710
Hey Jagwrman,

run this code on the selection    make sure you run the macro called remDupstringsComma

Sub remDupstringsComma()
   ' Comma Separator
Dim dic As Object, cell As Range, temp As Variant
Dim i As Long
Set dic = CreateObject("scripting.dictionary")
With dic
    For Each cell In Selection
        .RemoveAll
        If Len(cell.Value) > 0 Then
            temp = Split(" " & cell.Value, ",")
            For i = 0 To UBound(temp)
                If Not .Exists(temp(i)) Then .Add temp(i), temp(i)
            Next i
            cell.Value = Mid(Join(.Keys, ","), 2)
        End If
    Next cell
End With
        Call RemoveDuplicateRows
End Sub

Sub RemoveDuplicateRows()
    Application.ScreenUpdating = False
    Dim ColN As Long
    Dim MyS As Worksheet: Set MyS = ActiveSheet
    Dim MyR As Range: Set MyR = MyS.Cells(1, 1).CurrentRegion
    Dim NumCol As Long: NumCol = MyR.Columns.Count
    Dim MyArray As Variant: ReDim MyArray(0 To NumCol - 1)
    For ColN = 1 To NumCol
        MyArray(ColN - 1) = ColN
    Next
    MyR.RemoveDuplicates Columns:=(MyArray), Header:=xlYes
    Dim rowcount As Long, i As Long, j As Long, k As Boolean
    rowcount = MyR.Rows.Count
    For i = rowcount To 1 Step -1
        k = 0
        For j = 1 To NumCol
            If MyR.Value2(i, j) <> "" Then
                k = 1
                Exit For
            End If
        Next j
        If k = 0 Then
            MyR.Rows(i).Delete Shift:=xlUp
        End If
    Next i
    Application.ScreenUpdating = True
End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 250 total points
ID: 40400021
Sub kTest()
    
    Dim k, e, v, i As Long
    
    k = Range("a1").CurrentRegion.Value2
    
    With CreateObject("scripting.dictionary")
        .comparemode = 1
        For Each e In k
            v = Split(e, ",")
            For i = 0 To UBound(v)
                If Len(v(i)) Then .Item(Trim(v(i))) = Empty
            Next
        Next
        If .Count Then
            Worksheets.Add
            Range("a1").Value = Join(.keys, ",")
        End If
    End With
    
End Sub

Open in new window


Kris
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

1 Experts available now in Live!

Get 1:1 Help Now