+ Reply to Thread
Results 1 to 3 of 3

Comparing Two Columns in Excel Row By Row?

Hybrid View

  1. #1
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Comparing Two Columns in Excel Row By Row?

    I have currently browsed the forums and have came up with a code to compare two columns from two separate excel books and then highlight anything matching with the CompareRange. Here is a few more details about the problem:

    - I have two excel sheets. And data like this in each sheet:

    (First Sheet) (Second Sheet)
    •A B N O
    •7 .7 3 .56
    •6 .6 8 .45
    •5 .5 9 .55
    •4 .4 11 .2
    •3 .3 8 .22
    •2 .2 9 .55
    •1 .1 8 .54

    As you can see, given this example nothing should be highlighted once the macro is run since nothing from Column A or B from the first sheet matches directly with Column N & O from the second sheet. The problem is that with the macro (module) I have come up with will highlight "3" from Column A and ".2" from Column B, just because they appear in Column N & Column O respectivally.

    What I want: I only want a number to be highlighted if both the numbers "7" & ".7" are matched in the same row of Column N & Column O on the other spreadsheet. I have no idea how to do this. To be a little more precise, I'll give an example. Say I edited the data to be like this.

    (First Sheet) (Second Sheet)
    •A B N O
    •7 .7 3 .56
    •8 .45 8 .45
    •5 .5 9 .55
    •11 .4 11 .2
    •3 .3 8 .22
    •2 .2 9 .55
    •1 .1 8 .54

    With this data, I would want the second row of A & B ("8" & ".45") highlighted, while my error "3" of Column A and ".2" of Column B is not highlighted. Also, I would like it if row 4 of Column A & B ("11" & ".4") is not highlighted at all either, just because in O it is .2 and in B it would be .4 even though the 11's match.

    Please advise. Thanks in advance.

    Attached is the macro/module I have entered in which is working kind of correctly but producing the mistake.

    And also, (kind of a lesser problem), both the files with data will have the same header, example would be if Column A & Column N both had "Dogs" as it's title in Row 1 and Column B & O both had "Cats" as it's title in Row 1. Is there anyway the macro can be adjusted so it compares those two columns between the two workbooks without me even having to select or assigning a range? Thank you so much.

    Sub Find_Matches()
        Dim Column1 As Range
        Dim Column2 As Range
        Set Column1 = Application.InputBox("Select First Column to Compare", Type:=8)
        If Column1.Columns.Count > 1 Then
            Do Until Column1.Columns.Count = 1
                MsgBox "You can only select 1 column"
                Set Column1 = Application.InputBox("Select First Column to Compare", Type:=8)
            Loop
        End If
            Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
        If Column2.Columns.Count > 1 Then
          Do Until Column2.Columns.Count = 1
            MsgBox "You can only select 1 column"
            Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
          Loop
        End If
          If Column2.Rows.Count <> Column1.Rows.Count Then
        Do Until Column2.Rows.Count = Column1.Rows.Count
          MsgBox "The second column must be the same size as the first"
          Set Column2 = Application.InputBox("Select Second Column to Compare", Type:=8)
        Loop
      End If
      If Column1.Rows.Count = 65536 Then
        Set Column1 = Range(Column1.Cells(1), Column1.Cells(ActiveSheet.UsedRange.Rows.Count))
        Set Column2 = Range(Column2.Cells(1), Column2.Cells(ActiveSheet.UsedRange.Rows.Count))
      End If
        Dim CompareRange As Variant, x As Variant, y As Variant
        ' Set CompareRange equal to the range to which you will
        ' compare the selection.
        Set CompareRange = Workbooks("Book4").Worksheets("Sheet1").Range("N2:N7")
        Set CompareRange1 = Workbooks("Book4").Worksheets("Sheet1").Range("O2:O7")
        ' NOTE: If the compare range is located on another workbook
        ' or worksheet, use the following syntax.
        ' Set CompareRange = Workbooks("Book2"). _
        '   Worksheets("Sheet2").Range("C1:C5")
        '
        ' Loop through each cell in the selection and compare it to
        ' each cell in CompareRange.
        For Each x In Column1
            For Each y In CompareRange
                If x = y Then
                x.Interior.Color = vbYellow
                End If
                
                'x.Offset(0, 5) = x
            Next y
        Next x
        For Each x In Column2
            For Each y In CompareRange1
                If x = y Then
                x.Interior.Color = vbYellow
                End If
                'x.Offset(0, 5) = x
            Next y
        Next x
    End Sub

  2. #2
    Registered User
    Join Date
    06-05-2013
    Location
    New York
    MS-Off Ver
    Excel 2010
    Posts
    39

    Re: Comparing Two Columns in Excel Row By Row?

    To expand on what I need, I need this Macro to be able to check if A1 appears anywhere in column N (say it shows up at N4), and if it does then go on to see if O4 matches B1. Only then, do I want cell A1 & B1 highlighted. Please advise.

  3. #3
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,625

    Re: Comparing Two Columns in Excel Row By Row?

    Option Explicit
    Sub comparecols()
        Dim BaseCol As Range, _
            RelCol As Range, _
            MatchRow, _
            TestCell    As Range
        
        Set BaseCol = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
        Set RelCol = Range("N1:N" & Cells(Rows.Count, "N").End(xlUp).Row)
        
        For Each TestCell In BaseCol
            MatchRow = Application.Match(TestCell.Value, RelCol, 0)
            If Not IsError(MatchRow) Then
                If TestCell.Offset(0, 1).Value = Range("O" & MatchRow).Value Then
                    TestCell.Resize(1, 2).Interior.ColorIndex = 8
                End If
            End If
        Next TestCell
            
    End Sub
    Last edited by protonLeah; 06-06-2013 at 01:43 AM.
    Ben Van Johnson

+ 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