+ Reply to Thread
Results 1 to 2 of 2

Compare columns edit VBA code

Hybrid View

  1. #1
    Registered User
    Join Date
    04-10-2015
    Location
    Massachusetts, USA
    MS-Off Ver
    MS Office 2010 Pro
    Posts
    4

    Compare columns edit VBA code

    I copied this macro from the Microsoft website. It compares two columns of data for matches in both and then returns the matches in Column "B". What I'd like it to do is prompt for the first column, say "G" and the second column, say "I". I'd also like it to prompt what column the matches should be put in. It defaults to putting the matches in Column B but if I have data in Column A and B that must be near each other it makes it so I can't use this macro. Please ask for clarification if needed! Thanks.

    Sub Find_Matches()
        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 = Range("C1:C5")
        ' 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 Selection
            For Each y In CompareRange
                If x = y Then x.Offset(0, 1) = x
            Next y
        Next x
    End Sub

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    13,000

    Re: Compare columns edit VBA code

    Try this one:
    Option Explicit
    Sub Find_Matches()
        Dim CompareColumn   As Range, _
            CompareCell     As Range, _
            TestColumn      As Range, _
            TestCell        As Range, _
            ResultColumn    As Range, _
            CompareRows     As Long, _
            DestinationRow  As Long, _
            FoundVal        As Variant
            
            
        Set CompareColumn = Application.InputBox("Click in the  * COLUMN * to TEST", "Select TEST column", Type:=8)
        Set TestColumn = Application.InputBox("Click the column to compare to", "Select Compare column", Type:=8)
        Set ResultColumn = Application.InputBox("Click the column for the RESULTS", "Select RESULT column", Type:=8)
        CompareRows = WorksheetFunction.Max(Cells(Rows.Count, CompareColumn.Column).End(xlUp).Row, Cells(Rows.Count, TestColumn.Column).End(xlUp).Row)
            
        Set CompareColumn = CompareColumn.Resize(rowsize:=CompareRows)
        Set TestColumn = TestColumn.Resize(rowsize:=CompareRows)
        
        For Each TestCell In TestColumn
            If TestCell <> "" Then
                Set FoundVal = CompareColumn.Find(TestCell.Value)
                If FoundVal Is Nothing Then
                    DestinationRow = DestinationRow + 1
                    
                    Cells(DestinationRow, ResultColumn.Column).Value = TestCell.Value
                End If 'try to find testcell value
            End If  'current test cell
        Next TestCell
    End Sub
    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)

Similar Threads

  1. Need VBA Code to compare Values in two columns
    By Karthik Sen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-11-2014, 05:20 PM
  2. Help Edit Code to Search Three Columns
    By bixa99 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-05-2014, 07:51 PM
  3. VBA - Code to compare 2 columns
    By Swiss Cheese in forum Excel Programming / VBA / Macros
    Replies: 23
    Last Post: 08-08-2012, 05:26 AM
  4. Help with VB code to compare two columns
    By Farooq Sheri in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2006, 07:20 AM
  5. How do I write a VBA code in excel that will compare two columns .
    By PenelopeinCinci in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-14-2005, 01:06 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