+ Reply to Thread
Results 1 to 5 of 5

Macro to delete duplicate cell without sorting

Hybrid View

  1. #1
    Registered User
    Join Date
    07-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    21

    Macro to delete duplicate cell without sorting

    Looking for a macro to satisfy the below my requirement.

    Example Data:
    ColumnA ColumnB ColumnC
    Jane 500 Female
    Jane 200 Female
    Mike 500 Male
    Mike 200 Male

    Expected Data:
    ColumnA ColumnB ColumnC
    Jane 500 Female
    200
    Mike 500 Male
    200

  2. #2
    Forum Contributor vikas.bhandari's Avatar
    Join Date
    04-07-2006
    Location
    Delhi, India
    MS-Off Ver
    Office 2007 and 2010
    Posts
    303

    Re: Macro to delete duplicate cell without sorting

    Hi there!

    Assuming you are running it on Data sorted by ColumnA, the code would be :

    
    Public Sub DeleteDuplicates()
        Dim lastRow As Long
        Dim rowCounter As Long
        
        lastRow = ActiveSheet.Range("A65000").End(xlUp).Row
        
        For rowCounter = 2 To lastRow
            If ActiveSheet.Range("A" & rowCounter).value <> "" Then
                ClearDuplicateCells ActiveSheet.Range("A" & rowCounter)
            End If
        Next rowCounter
        
    End Sub
    
    Public Sub ClearDuplicateCells(currentCell As Range)
        Dim rng As Range
        Dim sht As Worksheet
        Dim value
        Dim lastRow As Long
        Dim c As Range
        Dim runLoop As Boolean
        Dim unionRng As Range
         
        value = currentCell.value
        Set unionRng = currentCell
        Set sht = currentCell.Parent
        Set rng = sht.Range(currentCell.Offset(0, 0), sht.Cells(currentCell.Row + 100000, currentCell.Column))
         
        lastRow = 0
        runLoop = True
         
        With rng
            Set c = .Find(value, LookIn:=xlValues)
             
            If Not c Is Nothing Then
                 
                
                While runLoop
                    If Not c Is Nothing Then
                        If c.Row > lastRow Then
                            c.ClearContents
                            lastRow = c.Row
                            Set c = .FindNext(c)
                        Else
                            runLoop = False
                        End If
                    Else
                        runLoop = False
                    End If
                Wend
            Else
                '''MsgBox "Didnt find the value"
            End If
        End With
         
        Dim newRange As Range
        
         
         
    End Sub
    However, if the data is not sorted, it won't really make much sense to remove duplicates.

    Thanks,
    Vikas B

  3. #3
    Registered User
    Join Date
    07-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Macro to delete duplicate cell without sorting

    Thanks Vikas for quick reply, hoever I am getting "Run-time error '1004'". When i debug it is higlighting the below code. I am still new to macro, your help would be appreciated.

    Set rng = sht.Range(currentCell.Offset(0, 0), sht.Cells(currentCell.Row + 100000, currentCell.Column))

  4. #4
    Forum Contributor vikas.bhandari's Avatar
    Join Date
    04-07-2006
    Location
    Delhi, India
    MS-Off Ver
    Office 2007 and 2010
    Posts
    303

    Re: Macro to delete duplicate cell without sorting

    Hi,

    Your workbook may be in compatibility mode which may not have 100000 rows. So use this :

    
    Set rng = sht.Range(currentCell.Offset(0, 0), sht.Cells(currentCell.Row + 60000, currentCell.Column))

  5. #5
    Registered User
    Join Date
    07-17-2012
    Location
    Bangalore
    MS-Off Ver
    Excel 2010
    Posts
    21

    Re: Macro to delete duplicate cell without sorting

    Thanks, that was easy one . Now I am able to get the result what I wanted in ColumnA but my data range is A:BQ, do I have to modify for each column and run the macro or is there anything which can run at one shot?

    Regards,
    Chito

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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