Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: VBA Program to Compare 4 Columns in Excel (Required)

  1. #11
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi Vijay,

    Please do not quote the entire post unless it is relevant.
    You may also try this code.

    Code:
    Option Explicit
    
    Sub kTest()
        
        Dim ResultCol           As Long
        Dim Cols2Compare        As String
        Dim d, i As Long, Dic() As Object
        Dim x, j As Long, UB    As Long
        
        
        Cols2Compare = Application.InputBox("Enter the columns to compare", "Compare Columns", "1,3,5,6", Type:=2)
        
        If Cols2Compare = "False" Or Cols2Compare = "" Then Exit Sub
        
        ResultCol = Application.InputBox("Enter the result column", "Compare Columns", 10, Type:=1)
        
        If ResultCol = 0 Then Exit Sub
        
        x = Split(Cols2Compare, ",")
        UB = UBound(x)
        If UB < 1 Then
            MsgBox "Minimum 2 columns required", vbInformation
            Exit Sub
        End If
        
        For i = 0 To UB
            ReDim Preserve Dic(i)
            Set Dic(i) = CreateObject("scripting.dictionary")
            Dic(i).comparemode = 1
        Next
        
        d = Range("a1").CurrentRegion.Value2
        
        For j = 0 To UB
            For i = 1 To UBound(d, 1) 'replace 1 with 2 if the data have column headers
                Select Case j
                    Case 0
                        If Len(d(i, x(j))) Then
                            Dic(0).Item(d(i, x(j))) = True
                        End If
                    Case Else
                        If Dic(j - 1).exists(d(i, x(j))) Then
                            Dic(j).Item(d(i, x(j))) = True
                        End If
                End Select
            Next
        Next
        
        If Dic(UB).Count Then
            j = Dic(UB).Count
            Cells(1, ResultCol) = "Result"
            Cells(2, ResultCol).Resize(j) = Application.Transpose(Dic(UB).keys)
            Cells(2, ResultCol).Resize(j).Sort Cells(2, ResultCol), 1
        End If
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #12
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Thank you so much for your kind assistance..It helps me to save a lot of time :-)

Similar Threads

  1. Absolute Calender Program!
    By Preeti Verma in forum Excel Help
    Replies: 1
    Last Post: 11-06-2012, 01:19 PM
  2. Validating 2 Columns using excel VBA
    By freakszzy in forum Excel Help
    Replies: 2
    Last Post: 07-26-2012, 12:46 PM
  3. compare column a to b and b to a
    By ayazgreat in forum Excel Help
    Replies: 18
    Last Post: 05-07-2012, 04:46 PM
  4. Replies: 1
    Last Post: 11-11-2011, 02:06 PM
  5. Compare two worksheets and List Differences
    By excel_learner in forum Excel Help
    Replies: 1
    Last Post: 11-02-2011, 10:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •