+ Reply to Thread
Results 1 to 13 of 13

Delete Duplicate Records

Hybrid View

Foxcan Delete Duplicate Records 07-15-2009, 04:51 PM
JBeaucaire Re: Delete Duplicate Records 07-15-2009, 04:57 PM
JBeaucaire Re: Delete Duplicate Records 07-15-2009, 05:25 PM
Foxcan Re: Delete Duplicate Records 07-16-2009, 03:14 PM
martindwilson Re: Delete Duplicate Records 07-15-2009, 05:43 PM
JBeaucaire Re: Delete Duplicate Records 07-15-2009, 08:59 PM
martindwilson Re: Delete Duplicate Records 07-16-2009, 05:23 AM
Cjax Re: Delete Duplicate Records 07-17-2009, 11:08 AM
JBeaucaire Re: Delete Duplicate Records 07-17-2009, 02:39 PM
Cjax Re: Delete Duplicate Records 07-17-2009, 03:23 PM
JBeaucaire Re: Delete Duplicate Records 07-17-2009, 10:41 PM
NBVC Re: Delete Duplicate Records 07-17-2009, 03:35 PM
Cjax Re: Delete Duplicate Records 07-18-2009, 09:56 PM
  1. #1
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Delete Duplicate Records

    Quote Originally Posted by Cjax View Post
    If I browse the forums just to learn techniques should I start a new thread everytime I have a question about an author's answer. I didn't think trying to understand a thread was the same as having my own situation i need help with.
    Of course, you are correct. I apologize.

    And one of the things i *really* need to do better is put comments into the code itself to explain what's going on.

    Ok, let's start with your original question (which is what sounded like a topic change to me...)..."how to add a macro".

    ==========
    How to use add a macro:

    1. Open up your workbook
    2. Get into VB Editor (Press Alt+F11)
    3. Insert a new module (Insert > Module)
    4. Copy and Paste in your code (given above)
    5. Get out of VBA (Press Alt+Q)
    6. Save your sheet

    The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.
    ==========

    If that's all you needed, great. If not, we can talk about the specifics of the macro itself. Here's the second macro with more "notes" added:
    Sub DeleteDupesOnly()
    'Deletes duplicates leaving complete list of unique values only
    
    'Declare a "last row" variable, always use "Long" for row variables
        Dim LR As Long
    
    'Data needs to start at row 2, so we'll insert a row
        Rows(1).Insert (xlShiftDown)
    
    'Put a value into the LR variable, this tells us how for down the data goes
        LR = Range("B" & Rows.Count).End(xlUp).Row
        
    'Insert a formula in column AA starting at AA2 =COUNTIF($AA$2:AA2,AA2)
    'This will give a count of how many times the value in column A has occurred
    'so far in the column down to this row, duplicates will have higher and higher
    'numbers as the formulas go down that column
        Range("AA2:AA" & LR).FormulaR1C1 = "=COUNTIF(R2C2:RC2,RC2)"
    
    'Insert an Autofilter in column AA and show all rows with values
    'greater than 1. This will be all the duplicates after the first
    'instance of each value
        Range("AA1") = "Key"
        Range("AA1").AutoFilter Field:=1, Criteria1:=">1"
    
    'Delete the visible rows, since the unique values have been hidden this is OK
        Range("AA2:AA" & LR).SpecialCells(xlCellTypeVisible).EntireRow.Delete
        
    'Cleanup, remove the column AA we added and the row 1 we added
        Rows(1).Delete (xlShiftUp)
        Columns("AA:AA").Clear
    End Sub
    Last edited by JBeaucaire; 07-17-2009 at 10:44 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

+ 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