+ Reply to Thread
Results 1 to 5 of 5

Compare 2 columns and mark the duplicates

Hybrid View

powpowninjastar Compare 2 columns and mark... 03-12-2010, 08:16 PM
Leith Ross Re:... 03-12-2010, 08:36 PM
powpowninjastar Re:... 03-12-2010, 08:43 PM
Leith Ross Re: Compare 2 columns and... 03-12-2010, 09:53 PM
powpowninjastar Re: Compare 2 columns and... 03-12-2010, 10:06 PM
  1. #1
    Registered User
    Join Date
    03-11-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Compare 2 columns and mark the duplicates

    I was wondering if there is anyway I could do something similar to Cells.Find(What:=Paste) in a macro. I want to compare two columns of cells, one on the first worksheet and the other on the second worksheet, and identify which one from the first sheet are in the second one.

    I tried recording a macro that would:
    1) take the first cell from the first column
    2) copy it (Ctrl+C)
    3) go to the second sheet
    4) paste it (Ctrl+V) in the "find" dialog box .. ***
    5) turn on relative references and press tabs to select the cell right next to the "found" cell
    6) type an "X"... or anything i'd like.. Shipped, received, missing blabla
    7) go back to the first sheet
    8) with relative references still turned on go down one cell
    9)and then loop the macro from step 2 till it reaches the last filled cell from the first column in the first sheet.

    ***That's what bugging me, Excel won't record "Ctrl+V" in the macro, so it would keep looking for the values it first pasted.. let's say that when I recorded the macro i "Ctrl+C" the first cell which contains "1", it would loop but instead of "Ctrl+V" the newly "Ctrl+C" value, it would keep looking for the "1"...

    So it didn't work..
    I'm still new with macros so google is my best pal at the momment but it seems google has its limits.

    Before Macro
    Sheet 1

    2
    3

    Sheet 2

    1
    2
    3
    4

    After Macro
    Sheet 1

    2
    3

    Sheet 2

    1
    2 x
    3 x
    4

    That's pretty much what i'd like to do.

    So if anybody knows of a better way to achieve such a thing, I'd be more than pleased to hear it.
    Last edited by powpowninjastar; 03-12-2010 at 10:07 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Cells.Find(What:="Paste?")

    Hello powpowninjastar,

    Welcome to the Forum!

    Your title does not accurately reflect your question. Please change it. Thread titles should be in a format suitable to database searches. I would suggest "Compare 2 columns and mark the duplicates"
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    03-11-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Cells.Find(What:="Paste?")

    Ok, title changed
    Last edited by powpowninjastar; 03-12-2010 at 08:51 PM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Compare 2 columns and mark the duplicates

    Hello powpowninjastar,

    Thank you for changing the title. You can change the sheet names and the starting cells in the code to match what you will be using. Copy this code to a standard VBA module in your workbook.
    Sub CompareColumns()
    
      Dim Cell As Range
      Dim DSO As Object
      Dim Key As String
      Dim Item As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Sht1 As Worksheet
      Dim Sht2 As Worksheet
      
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
        Set Sht1 = Worksheets("Sheet1")
        Set Sht2 = Worksheets("Sheet2")
        
          Set Rng = Sht1.Range("A1")
          Set RngEnd = Sht1.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Sht1.Range(Rng, RngEnd))
          
          For Each Cell In Rng
            Key = Trim(Cell.Text)
            If Key <> "" Then
              If Not DSO.Exists(Key) Then DSO.Add Key, 0
            End If
          Next Cell
          
          Set Rng = Sht2.Range("A1")
          Set RngEnd = Sht2.Cells(Rows.Count, Rng.Column).End(xlUp)
          Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Sht2.Range(Rng, RngEnd))
          
          For Each Cell In Rng
            Key = Trim(Cell.Text)
            If Key <> "" Then
              If DSO.Exists(Key) Then Cell.Offset(0, 1) = "x"
            End If
          Next Cell
          
        Set DSO = Nothing
        
    End Sub

  5. #5
    Registered User
    Join Date
    03-11-2010
    Location
    Montreal, Canada
    MS-Off Ver
    Excel 2003
    Posts
    8

    Re: Compare 2 columns and mark the duplicates

    Well thanks a lot, that works perfectly and being able to change the "x" will truly be helpful.

+ 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