+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Forum Contributor
    Join Date
    02-03-2009
    Location
    bangalore, india
    MS-Off Ver
    Excel 2003
    Posts
    101

    Compare columns in excel

    have attached a sheet all i want is to have the column b in the sheet
    with values that are not common in column a and column c

    in other words if "audit_payment" appears in both colA and colC it should not come in Colb

    pls help
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    11-23-2005
    Location
    Rome
    MS-Off Ver
    Ms Office 2003
    Posts
    1,227

    Re: Compare columns in excel

    You could use this formula to put in cells B1:Bn:
    =IF(ISNA(MATCH(A1,C:C,0)),A1,"")
    Regards,
    Antonio

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,496

    Re: Compare columns in excel

    Hello maximpinto,

    The attached workbook has a button on Sheet1 to run the macro. The macro will compare the columns "A" and "C". Only values unique to each are list in column "B". The starting row is row 3. If you have a large data set, this is the fastest method. You won't lose time copying formulas and the execution is very fast.
    Code:
    'Written: March 18, 2010
    'Author:  Leith Ross
    
    Sub ListUniques()
    
      Dim Cell As Range
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim R As Long
      Dim Rng As Range
      Dim RngA As Range
      Dim RngB As Range
      Dim RngC As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Sheet1")
        
        Set Rng = Wks.Range("A3"): GoSub SizeRange: Set RngA = Rng
        Set Rng = Wks.Range("B3"): GoSub SizeRange: Set RngB = Rng
        Set Rng = Wks.Range("C3"): GoSub SizeRange: Set RngC = Rng
        
          Set DSO = CreateObject("Scripting.Dictionary")
          DSO.CompareMode = vbTextCompare
          
          Set Rng = Union(RngA, RngC)
          
          For Each Cell In Rng
            If Not IsEmpty(Cell.Value) Then
              Key = Trim(Cell.Value)
              If Not DSO.Exists(Key) Then
                DSO.Add Key, 1
              Else
                DSO.Remove Key
              End If
            End If
          Next Cell
          
       RngB.ClearContents
       RngB.Resize(DSO.Count, 1) = WorksheetFunction.Transpose(DSO.Keys)
       
       Set DSO = Nothing
       Exit Sub
       
    SizeRange:
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
        Return
        
    End Sub
    Attached Files Attached Files
    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!)

  4. #4
    Forum Contributor
    Join Date
    02-03-2009
    Location
    bangalore, india
    MS-Off Ver
    Excel 2003
    Posts
    101

    Re: Compare columns in excel

    Thanks Leith for the code, it worked fine

    One more questions if i want to select the common ones in comparing both ColA and ColB and put them in Col D. Is it a small change in the code or a big one. If it takes time its ok no isssue
    but let me know so that i can close this post as solved
    thanks
    max

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,496

    Re: Compare columns in excel

    Hello Max,

    I modified the macro to list both uniques and duplicates. Here is the new macro code. This has been added to the attached workbook.
    Code:
    'Written: March 19, 2010
    'Author:  Leith Ross
    'Summary: Compare columns "A" and "C". List uniques in column "B" and duplicates in column "D"
    
    Sub ListUniques()
    
      Dim Cell As Range
      Dim Dupes() As Variant
      Dim Key As Variant
      Dim Keys As Variant
      Dim LastRow As Long
      Dim N As Long
      Dim Rng As Range
      Dim RngA As Range
      Dim RngB As Range
      Dim RngC As Range
      Dim RngD As Range
      Dim RngEnd As Range
      Dim Uniques As Object
      Dim Wks As Worksheet
      
        Set Wks = Worksheets("Sheet1")
        
        Set Rng = Wks.Range("A3"): GoSub SizeRange: Set RngA = Rng
        Set Rng = Wks.Range("B3"): GoSub SizeRange: Set RngB = Rng
        Set Rng = Wks.Range("C3"): GoSub SizeRange: Set RngC = Rng
        Set Rng = Wks.Range("D3"): GoSub SizeRange: Set RngD = Rng
        
          Set Uniques = CreateObject("Scripting.Dictionary")
          Uniques.CompareMode = vbTextCompare
          
          Set Rng = Union(RngA, RngC)
          
          For Each Cell In Rng
            If Not IsEmpty(Cell.Value) Then
              Key = Trim(Cell.Value)
              If Not Uniques.Exists(Key) Then
                Uniques.Add Key, 1
              Else
                Uniques.Remove Key
              End If
            End If
          Next Cell
          
       RngB.ClearContents
       RngB.Resize(Uniques.Count, 1) = WorksheetFunction.Transpose(Uniques.Keys)
       
          For Each Cell In Rng
            If Not IsEmpty(Cell.Value) Then
              Key = Trim(Cell.Value)
              If Not Uniques.Exists(Key) Then
                N = N + 1
                ReDim Preserve Dupes(1 To 1, 1 To N)
                Dupes(1, N) = Key
              End If
            End If
          Next Cell
          
       RngD.ClearContents
       RngD.Resize(N, 1) = WorksheetFunction.Transpose(Dupes)
       
       Set Uniques = Nothing
       Exit Sub
       
    SizeRange:
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
        Return
        
    End Sub
    Attached Files Attached Files
    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!)

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.2.0