+ Reply to Thread
Results 1 to 9 of 9

Matching columns

Hybrid View

lovinguy Matching columns 09-14-2013, 07:58 PM
GC Excel Re: Matching columns 09-14-2013, 10:11 PM
lovinguy Re: Matching columns 10-06-2013, 05:32 PM
jindon Re: Matching columns 09-15-2013, 01:20 AM
GC Excel Re: Matching columns 10-06-2013, 10:59 PM
lovinguy Re: Matching columns 10-08-2013, 06:15 AM
GC Excel Re: Matching columns 10-08-2013, 05:17 PM
lovinguy Re: Matching columns 10-08-2013, 07:38 PM
GC Excel Re: Matching columns 10-08-2013, 10:34 PM
  1. #1
    Forum Contributor
    Join Date
    10-21-2010
    Location
    LONDON
    MS-Off Ver
    2007
    Posts
    108

    Matching columns

    Hi!

    I want to create a MACRO so that it gives a option or button to select which column I want to match while comparing SHEET1 with SHEET2.Like FNAME & LNAME or all the columns or any of the 4 columns.The matched rows should get filled in sheet "Result".

    I know vlookUP is there but I do have large sets of data.Attached is the sample worksheet.

    Thanks in advance.


    Lovinguy
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Matching columns

    Hi,
    Try this code :
    You input the column number to compare separated by comma : 1,2,3,...
    use -1 to compare all

    Sub CompareColumns()
       Dim ar1, ar2, arCol
       Dim Dic1
       Dim i As Long, ii As Long, k As Long, n As Long
       Dim col As String, temp As String
       
       On Error GoTo errhandler
    
       ar1 = Sheets(1).Cells(1).CurrentRegion
       ar2 = Sheets(2).Cells(2).CurrentRegion
    
       Set Dic1 = CreateObject("Scripting.Dictionary")
    
       ' / Get columns to compare
       col = InputBox("Columns to compare separated by comma (-1 for all)" & vbLf & _
                      "(example : 1,2,4)", "Columns number to compare", "-1")
       col = Replace(col, " ", "")
       
       ' / Build array of columns to compare
       If col = "-1" Then      '/ all columns
          ReDim arCol(0 To UBound(ar1, 2) - 1)
          For i = 0 To UBound(ar1, 2) - 1
             arCol(i) = i + 1
          Next i
       Else
          arCol = Split(col, ",")
       End If
    
       ' / Create dictionary
       For i = 2 To UBound(ar1, 1)
          temp = ""
          For k = 0 To UBound(arCol)
             temp = temp & ar1(i, arCol(k)) & Chr(2)
          Next k
          If Not Dic1.exists(temp) Then Dic1.Add temp, i
       Next i
    
       ' / With data in sheet2, look if exists in sheet1
       n = 2
       For i = 2 To UBound(ar2, 1)
          temp = ""
          For k = 0 To UBound(arCol)
             temp = temp & ar2(i, arCol(k)) & Chr(2)
          Next k
          If Dic1.exists(temp) Then     'if item exists, write back in array
             For ii = 1 To UBound(ar2, 2)
                ar1(n, ii) = ar2(i, ii)
             Next ii
             n = n + 1
          End If
       Next i
    
       ' / Write results in sheet 3
       With Sheets(3).Cells(1)
          .CurrentRegion.Clear
          .Resize(n - 1, UBound(ar1, 2)) = ar1
       End With
       
       Exit Sub
       
    errhandler:
       MsgBox "Execution error. Check if valid column numbers provided. " & vbLf & col
    
    End Sub
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Contributor
    Join Date
    10-21-2010
    Location
    LONDON
    MS-Off Ver
    2007
    Posts
    108

    Re: Matching columns

    Thanks GC Excel for the solution.

    I want to modify my requirement lit bit.Possible that Macro can Delete the duplicate records from Sheet1,rest of the requirement remains the same.

    Thanks once again

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Matching columns

    Try the attached
    Attached Files Attached Files

  5. #5
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Matching columns

    Hi,
    See if this code works in the attached file.
    Attached Files Attached Files

  6. #6
    Forum Contributor
    Join Date
    10-21-2010
    Location
    LONDON
    MS-Off Ver
    2007
    Posts
    108

    Re: Matching columns

    Thanks GC for your reply.

    I checked the Macro is not deleting the duplicate records from Sheet1.Although it is displaying the duplicate rows in sheet "Result".

    When I am clicking the blue button in sheet1.it is showing the error "Cannot run the macro "Sample with code 2.xlsm!test'.The macro may not be avialable in this workbook or all macros may be diabled".So I had to run this macro from VB module.

    Thanks

  7. #7
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Matching columns

    Hi,
    How do you define duplicate records ?
    Does all the columns must be the same or only specific columns ?
    In the current file you have all rows are different. (no duplicates)

  8. #8
    Forum Contributor
    Join Date
    10-21-2010
    Location
    LONDON
    MS-Off Ver
    2007
    Posts
    108

    Re: Matching columns

    As I said the above code which you provided me perfect.I just want to add another feature that is Deletion of duplicate records.

    The data which you have provided in "REsult" worksheet should get deleted from SHEET1.

    Thanks

  9. #9
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,414

    Re: Matching columns

    Ok... got it.
    Try the attached file.
    Attached Files Attached Files

+ 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: 2
    Last Post: 04-18-2013, 05:56 PM
  2. Align 3 columns with 2 columns of matching data
    By bearnbillie in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-04-2012, 08:33 AM
  3. Replies: 3
    Last Post: 06-09-2011, 05:58 AM
  4. sort columns based on matching 1st & 2nd columns
    By repke in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 02-13-2011, 01:37 PM
  5. compare columns and count number of matching columns
    By san000 in forum Excel General
    Replies: 1
    Last Post: 07-15-2009, 10:31 PM

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