+ Reply to Thread
Results 1 to 9 of 9

VBA Remove Duplicate Values From Multiple Columns

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    VBA Remove Duplicate Values From Multiple Columns

    Hi, I wonder whether someone could help me please.

    I'm using the code below to remove duplicate values from a column of cells, in this case column B.

    Sub RemoveDuplicateCells(ws As Worksheet)
        
        Dim i As Long
        Dim LR As Long
        
        Application.ScreenUpdating = False
        
        With ws
            LR = ws.Range("C" & Rows.Count).End(xlUp).Row
            For i = LR To 8 Step -1
                With ws.Range("B" & i)
                    If WorksheetFunction.CountIf(ws.Columns("B"), .Value) > 1 Then .ClearContents
                End With
                Next i
            End With
        End Sub
    The code works fine, but I'd now like to adapt this so I can remove the duplicates from columns B, C, D and F.

    I've tried over the last few days different methods but recieve 'Debug' errors.

    I just wondered whether someone may be able to look at this please and offer some guidance on how I may achieve this.

    Many thanks and kind regards

    Chris

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: VBA Remove Duplicate Values From Multiple Columns

    What about deleting those cells?

    Try this...

    Sub RemoveDuplicateCells(ws As Worksheet)
    Dim i As Long, LR As Long
    
    Application.ScreenUpdating = False
    
    With ws
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        .Range("B1:B" & lrw).RemoveDuplicates 1, xlGuess
        .Range("C1:C" & lrw).RemoveDuplicates 1, xlGuess
        .Range("D1:D" & lrw).RemoveDuplicates 1, xlGuess
        .Range("F1:F" & lrw).RemoveDuplicates 1, xlGuess
    End With
        
    Application.ScreenUpdating = True
        
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Remove Duplicate Values From Multiple Columns

    Hi @Sixthsense, thank you for taking the time to come back to me about this.

    Unfortunately though, when I run the code I receive a Run time '1004' Method 'Range of Object'_Worksheet failed' error, with debug highlighting this line:

    .Range("B1:B" & lrw).RemoveDuplicates 1, xlGuess
    Many thanks and kind regards

    Chris

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,059

    Re: VBA Remove Duplicate Values From Multiple Columns

    Replace all instances of lrw with LR in the code.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Remove Duplicate Values From Multiple Columns

    HI @romperstomper, thank you for taking the time to reply to my post.

    I've made the changes as you kindly suggested, but unfortunately I receive a Run time error '438' Object doesn's support this poperty or method' error.

    Many thanks and kind regards

    Chris

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,059

    Re: VBA Remove Duplicate Values From Multiple Columns

    Are you running it on 2003? It won't work pre-2007.

    Do you want to remove duplicates from each column individually, or only remove rows where the combination of all specified columns is a duplicate?

  7. #7
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Remove Duplicate Values From Multiple Columns

    Hi @romperstomper thank you for coming back to me with this.

    I can confirm I'm running 2003 and I would like to remove duplicates from each column individually.

    Many thanks and kind regards

    Chris

  8. #8
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,059

    Re: VBA Remove Duplicate Values From Multiple Columns

    You could simply amend your original code to something like this:
    Sub RemoveDuplicateCells(ws As Worksheet)
        
        Dim i As Long
        Dim LR As Long
        Dim vColumns As Variant
        Dim vCol As Variant
        
        vColumns = Array("B", "C", "D", "F")
        
        Application.ScreenUpdating = False
        
        With ws
            LR = ws.Range("C" & Rows.Count).End(xlUp).Row
            For i = LR To 8 Step -1
                For Each vCol In vColumns
                With ws.Range(vCol & i)
                    If WorksheetFunction.CountIf(ws.Columns(vCol), .Value) > 1 Then .ClearContents
                End With
                Next vCol
            Next i
        End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    05-26-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2013
    Posts
    682

    Re: VBA Remove Duplicate Values From Multiple Columns

    Hi @romperstomper, thnak you very much for coming back tio me with this.

    The solution works perfectly, thank you.

    All the best and kind regards

+ 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. Replies: 3
    Last Post: 04-09-2014, 06:10 AM
  2. [SOLVED] Checking for 4 duplicate values and remove columns
    By keis386 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-15-2013, 05:56 AM
  3. [SOLVED] Remove duplicate rows by matching values across columns in Excel 2007
    By guest2013 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-15-2013, 12:00 AM
  4. excel macro to remove specific columns and rows + remove duplicate
    By garrywelson in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 01-17-2013, 12:03 PM
  5. VBA Remove duplicate entries accross multiple columns??
    By siegreen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-21-2010, 06:03 AM

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