Results 1 to 10 of 12

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,401
    Rep Power
    10
    Try this

    Code:
    Private Sub CommandButton1_Click()
    
        Dim lngColumnIndex() As Long
        Dim lngLoop As Long, lngSelected As Long, lngRows As Long
        Dim strColumnHeaders As String, strSelected As String
        Dim blnHoldsTrue As Boolean
        Const lngColumnHeaderRow As Long = 1
        Const lngColumnsToCompare As Long = 4
        
        On Error GoTo Err
        ReDim lngColumnIndex(1 To lngColumnsToCompare + 1)
        For lngLoop = 1 To ActiveSheet.UsedRange.Columns.Count
            strColumnHeaders = strColumnHeaders & lngLoop & " - " & Cells(lngColumnHeaderRow, lngLoop).Value & "|"
        Next lngLoop
        strColumnHeaders = "The column headers are " & vbLf & vbLf & Join(Split(strColumnHeaders, "|"), vbLf) & vbLf
        For lngLoop = 1 To lngColumnsToCompare
            For lngSelected = 1 To lngLoop - 1
                strSelected = strSelected & lngColumnIndex(lngSelected) & vbLf
            Next lngSelected
            lngColumnIndex(lngLoop) = InputBox(strColumnHeaders & strSelected & "Enter each column index one by one", "Column Compare")
            strSelected = "You have already selected:" & vbLf & vbLf
        Next lngLoop
        
        For lngSelected = 1 To lngLoop - 1
            strSelected = strSelected & lngColumnIndex(lngSelected) & vbLf
        Next lngSelected
        lngColumnIndex(lngLoop) = InputBox(strColumnHeaders & strSelected & "Enter column index where you want to show the comparison result", "Column Compare")
        lngRows = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
        blnHoldsTrue = True
        For lngRows = lngColumnHeaderRow + 1 To lngRows
            For lngLoop = 2 To lngColumnsToCompare
                blnHoldsTrue = blnHoldsTrue And (Cells(lngRows, lngColumnIndex(lngLoop)).Value = Cells(lngRows, lngColumnIndex(lngLoop - 1)).Value)
            Next lngLoop
            Cells(lngRows, lngColumnIndex(lngLoop)).Value = blnHoldsTrue
            blnHoldsTrue = True
        Next lngRows
        Exit Sub
    Err:    MsgBox "Either cancelled by user, or incorrect entry made." & vbLf & vbLf & "If neither of these, unexpected error!", vbOKOnly + vbInformation, ""
        
    End Sub
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    I tried the above VBA code but it is not working as expected.. It is finding the duplicate values only when all the rows having the same data.. suppose if a column A on cell A25 is having value 56 & B85 is having value 56 , C23 is having a value of 56 and d83 is having a value of 56 then the program is not finding it as duplicate value. It is matching as duplicate value only when A25 = B25=C25=D25 = 56. Can you please sort this problem
    Last edited by Excel Fox; 06-20-2013 at 11:50 PM. Reason: Removed Quote Tag

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
  •