+ Reply to Thread
Results 1 to 22 of 22

VBA code to search multiple columns and delete any duplicate cells (not rows)

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    VBA code to search multiple columns and delete any duplicate cells (not rows)

    I have tried doing this with excel formulas but have reached a point where I dont think I can make it work. I am not experienced using VBA code so any help is really appreciated.

    My Excel program (Excel 2010) currently has several columns and each column looks for and pulls data from a specific file on my computer. Then I need to delete any duplicate data entries, count the number of unique entries and track the changes through a chart. I have everything done except I cannot figure out (or find on the internet) a way to search in multiple columns (more than 2) and delete just the duplicate cells. I want to delete the cells in a way where there is one left. For example if the code 12gf is duplicated three time, I want to be left with one 12gf (it doesnt matter what column the original one is left in). Additionally, column length changes and they are not sorted. I have attempted to attach an image of an example file below. Thanks for all the help!


    1.PNG

  2. #2
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Why can't you join the 3 columns into 1 and use remove duplicates?
    1. Include a sample workbook with an example of the result you want
    2. Use [CODE] and [FORMULA] wrappers for your VBA code or excel formulas
    3. If your question has been answered, mark the thread as SOLVED and click on the "Add Rep" star to thank whoever helped you.

  3. #3
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    I could do that however, I want to do this automatically and I am not sure how to join the three columns. Online most ways are explained on how to merge columns, but I want to add them together and then check for duplicates. I want to be able to open my spreadsheet and look at my graphs and everything will update automatically. If anyone has a code to combine three columns and then remove duplicates automatically I am open to trying that for sure. Thanks!

  4. #4
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Yeah, there's definitely code to do that. Can you provide a sample workbook to use. I can't edit a picture

  5. #5
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Sure thing. An example is attached.
    Attached Files Attached Files

  6. #6
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Here, try this
    Sub CombineRemoveDups()
    Dim x As Long
    Application.Calculation = xlCalculationManual
    Sheets(2).Columns(1).Clear
    With Sheets(1)
        For x = 1 To .UsedRange.Columns.Count
            Sheets(2).Range("A50000").End(xlUp).Offset(1).Resize(WorksheetFunction.CountA(.Columns(x))).Value = .Columns(x).Value
        Next x
    End With
    Sheets(2).Columns(1).RemoveDuplicates 1, xlNo
    Application.Calculation = xlCalculationAutomatic
    End Sub

  7. #7
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    When I use the code with just the example I provided it works. However in my full document I have some other words in different cells and I keep getting an error when i try to use the code in my real workbook. I have attached my full example now. Sorry for not attaching it first, I didnt realize it was necessary that the labels be in the example.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-25-2014
    Location
    california, USA
    MS-Off Ver
    Excel 2013
    Posts
    20

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    so where is updated workbook?

  9. #9
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    It is attached in my previous post, essentially I just added my labels back into the workbook where in the first workbook I posted I simplified it to much and just had my data in the document.

  10. #10
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Try this
    Sub CombineRemoveDups()
    Dim x As Long
    Application.Calculation = xlCalculationManual
    Sheets(2).Columns(1).Clear
    With Sheets(1)
        For x = 1 To .UsedRange.Columns.Count
            Sheets(2).Range("A50000").End(xlUp).Offset(1).Resize(WorksheetFunction.CountA(.Columns(x))).Value = Range(.Cells(2, x), .Cells(50000, x).End(xlUp)).Value
        Next x
    End With
    Sheets(2).Columns(1).RemoveDuplicates 1, xlNo
    Application.Calculation = xlCalculationAutomatic
    End Sub

  11. #11
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    That worked except that I got N/A in one cell, I am not sure why any ideas? Attached is my result:
    Attached Files Attached Files

  12. #12
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Yes. When I changed the range to account for the headers, I didn't change the resizing. This code should fix that
    Sub CombineRemoveDups()
    Dim x As Long
    Application.Calculation = xlCalculationManual
    Sheets(2).Columns(1).Clear
    With Sheets(1)
        For x = 1 To .UsedRange.Columns.Count
            Sheets(2).Range("A50000").End(xlUp).Offset(1).Resize(WorksheetFunction.CountA(.Columns(x)) - 1).Value = Range(.Cells(2, x), .Cells(50000, x).End(xlUp)).Value
        Next x
    End With
    Sheets(2).Columns(1).RemoveDuplicates 1, xlNo
    Application.Calculation = xlCalculationAutomatic
    End Sub

  13. #13
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    I am still getting this error:

    Capture.PNG

    Is this an issue of the naming of my sheets and location in the sheets of the data? For example, does this code work no matter where the columns are or do they have to be in the first three columns (a,b,c) etc... Thanks

  14. #14
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    My code says to go to the first sheet and look in column1 through column(number of used columns on the sheet). So it is expecting the data to be in sheet 1 and in columns A:whatever. If your data is different, just change my code to match.

  15. #15
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    That Worked thank you! Also, I may eventually track several sheets, is it possible to loop this code for as many sheets I have and just change the sheet name/columns etc within each loop?

    So for example if I have sheets 1-3 with my data I want to run this code one time and within the code if will look in each sheet and populate sheet 4 with a column of data that has no duplicates.

    Thanks for the help I will definitely leave a positive rep.

  16. #16
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Yes, just do this:
    Sub CombineRemoveDups()
    Dim x&, i As Long
    Application.Calculation = xlCalculationManual
    Sheets(Sheets.Count).Columns(1).Clear
    For i = 1 To Sheets.Count - 1
        With Sheets(i)
            For x = 1 To .UsedRange.Columns.Count
                Sheets(Sheets.Count).Range("A50000").End(xlUp).Offset(1).Resize(WorksheetFunction.CountA(.Columns(x)) - 1).Value = Range(.Cells(2, x), .Cells(50000, x).End(xlUp)).Value
            Next x
        End With
    Next i
    Sheets(Sheets.Count).Columns(1).RemoveDuplicates 1, xlNo
    Application.Calculation = xlCalculationAutomatic
    End Sub
    Last edited by k64; 06-16-2014 at 01:55 PM.

  17. #17
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    I am getting the below error:

    Capture.PNG

    I also want it to come as each sheet having its own column so can I put the following inside the loop and change 1 to i?

    [Sheets(Sheets.Count).Columns(i).RemoveDuplicates 1, xlNo]

  18. #18
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    I can't see what error you're having. Yes, you can put it in the loop. It should look like this:
    Sub CombineRemoveDups()
    Dim x&, i As Long
    Application.Calculation = xlCalculationManual
    Sheets(Sheets.Count).Columns(1).Clear
    For i = 1 To Sheets.Count - 1
        With Sheets(i)
            For x = 1 To .UsedRange.Columns.Count
                Sheets(Sheets.Count).Cells(50000, i).End(xlUp).Offset(1).Resize(WorksheetFunction.CountA(.Columns(x)) - 1).Value = Range(.Cells(2, x), .Cells(50000, x).End(xlUp)).Value
            Next x
        End With
        Sheets(Sheets.Count).Columns(i).RemoveDuplicates 1, xlNo
    Next i
    Application.Calculation = xlCalculationAutomatic
    End Sub

  19. #19
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    I am attaching an expanded example workbook, I would like one new sheet that has one column per sheet and those column each have that sheet's list of unique data entries within it
    Attached Files Attached Files

  20. #20
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Try this
    Sub CombineRemoveDups()
    Dim x&, i&, r As Long
    Application.Calculation = xlCalculationManual
    Sheets(Sheets.Count).Cells.Clear
    For i = 1 To Sheets.Count - 1
        With Sheets(i)
            For x = 1 To .UsedRange.Columns.Count
                r = WorksheetFunction.CountA(.Columns(x)) - 1
                If r > 0 Then Sheets(Sheets.Count).Cells(50000, i).End(xlUp).Offset(1).Resize(r).Value = Range(.Cells(2, x), .Cells(50000, x).End(xlUp)).Value
            Next x
        End With
        Sheets(Sheets.Count).Columns(i).RemoveDuplicates 1, xlNo
    Next i
    Application.Calculation = xlCalculationAutomatic
    End Sub

  21. #21
    Registered User
    Join Date
    06-12-2014
    Posts
    19

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    Thats the one! Thanks a ton for your help, I should have thought of the expanded example first and saved us both some time, I learned for next time

  22. #22
    Valued Forum Contributor
    Join Date
    08-06-2013
    Location
    Detroit, Michigan
    MS-Off Ver
    Excel 2013
    Posts
    671

    Re: VBA code to search multiple columns and delete any duplicate cells (not rows)

    You're welcome. Glad I could help Yes, in general, try to make your samples as close to the real thing as possible.

+ 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. Deleting rows with duplicate cells across multiple columns
    By kaysee10man in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-25-2012, 10:50 PM
  2. [SOLVED] VBA code to delete duplicate rows based on multiple conditions
    By pjsween in forum Excel General
    Replies: 5
    Last Post: 06-27-2012, 01:15 PM
  3. Replies: 0
    Last Post: 06-12-2012, 02:00 PM
  4. VBA code to find duplicate values within multiple rows and columns
    By pcbpinoy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-27-2011, 09:04 PM
  5. Replies: 5
    Last Post: 08-16-2006, 02:05 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