+ Reply to Thread
Results 1 to 4 of 4

Finding Identical Values in Two Columns

Hybrid View

  1. #1
    Registered User
    Join Date
    10-04-2017
    Location
    London, England
    MS-Off Ver
    2016
    Posts
    8

    Finding Identical Values in Two Columns

    Hello,


    This time, I have to write a sub that checks if a value is duplicate in two columns. If it is, a message box should appear saying "blah." If it is not, a message box should appear saying "okay."


    I have written a code. However, when I run it, I always get the message box saying "okay." This should not happen because there are definitely identical values in the two columns. Any help fixing my code would be great.
    Sub Compare()
    Dim r1 As Range, r2 As Range, rng1 As Range, rng2 As Range
    Set r1 = Worksheets("Sheet1").Range("A2:A11")
    Set r2 = Worksheets("Sheet1").Range("B2:B11")
        For Each rng1 In r1
              For Each rng2 In r2
                   If rng1 = rng2 Then
                        MsgBox ("blah")
                   Else
                        MsgBox ("okay")
                   End If
              Next
         Next
    End Sub
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #3 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 10-06-2017 at 11:15 PM.

  2. #2
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,328

    Re: Finding Identical Values in Two Columns

    It's not true
    This is how your macro works:
    ...
    blah => A2 = B2
    blah => A2 = B3
    blah => A2 = B4
    okay => A2 <> B5 Not the same values
    blah => A2 = B6
    blah => A2 = B7
    blah => A2 = B8
    blah => A2 = B9
    blah => A2 = B10
    blah => A2 = B11
    blah => A3 = B2
    blah => A3 = B3
    blah => A3 = B4
    okay => A3 <> B5 Not the same values
    blah => A3 = B6
    blah => A3 = B7
    ...
    'and so on
    Attached Files Attached Files

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner VA USA
    MS-Off Ver
    MS 365 Family 64-bit 2505
    Posts
    27,643

    Re: Finding Identical Values in Two Columns

    You are comparing every cell in the A range to every cell in the B range and then giving a message for every comparison. You are showing 100 messages. What your code is doing is not what you described that you want it to do.

    If you want to check each value in the A range, and give blah if it has a dupe in B and okay if not, then do this. This will give you just 10 messages, one for every value in the A range. I also expanded your message a bit:

    Sub Compare()
    Dim r1 As Range, r2 As Range, rng1 As Range, rng2 As Range
    Dim Found As Range
    Set r1 = Worksheets("Sheet1").Range("A2:A11")
    Set r2 = Worksheets("Sheet1").Range("B2:B11")
        For Each rng1 In r1
              Set Found = r2.Find(what:=rng2, LookAt:=xlWhole, LookIn:=xlValues)
              If Found Is Nothing Then
                 MsgBox rng1.Address & " value " & rng1.Value & " is not found in range " & r2.Address
              Else
                 MsgBox rng1.Address & " value " & rng1.Value & " is found in " & Found.Address
              End If
         Next
    End Sub
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,568

    Re: Finding Identical Values in Two Columns

    Hi there,

    Here's one way:

    Option Explicit
    Sub Compare()
        
        Dim r1 As Range, r2 As Range
        Dim rngMyRange As Range
        Dim rngMyCell As Range
        Dim varMyResult As Variant
        
        Set r1 = Worksheets("Sheet1").Range("A2:A11")
        Set r2 = Worksheets("Sheet1").Range("B2:B11")
        
        Set rngMyRange = Worksheets("Sheet1").Range("A2:B11")
        
        For Each rngMyCell In rngMyRange
            If rngMyCell.Column = 1 Then
                varMyResult = Evaluate("VLookup(" & rngMyCell.Address & "," & r2.Address & ", 1, False)")
                If IsError(varMyResult) = False Then
                    MsgBox rngMyCell.Value & " is in the range " & r2.Address
                Else
                    MsgBox rngMyCell.Value & " is Not in the range " & r2.Address
                End If
            Else
                varMyResult = Evaluate("VLookup(" & rngMyCell.Address & "," & r1.Address & ", 1, False)")
                If IsError(varMyResult) = False Then
                    MsgBox rngMyCell.Value & " is in the range " & r1.Address
                Else
                    MsgBox rngMyCell.Value & " is Not in the range " & r1.Address
                End If
            End If
        Next rngMyCell
        
    End Sub
    Please use code tags when posting code as I have done. Thank you.

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

+ 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. Comparing two columns & showing identical values
    By Sham Sundar in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-18-2016, 07:46 AM
  2. Look up identical values in two seperate columns
    By nature718 in forum Excel General
    Replies: 1
    Last Post: 03-20-2014, 06:52 PM
  3. Finding similar but not identical values using COUNT and COUNTIF
    By krixtoffer in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-25-2011, 05:44 AM
  4. Finding similar, but not identical values in two-dimensional datasets
    By krixtoffer in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 10-24-2011, 04:51 PM
  5. [SOLVED] How do I match identical values in 2 columns and then sort?
    By Godswatch in forum Excel General
    Replies: 1
    Last Post: 11-09-2005, 03:00 PM
  6. Replies: 2
    Last Post: 05-17-2005, 01:32 PM
  7. Replies: 0
    Last Post: 05-14-2005, 03:19 PM

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