+ Reply to Thread
Results 1 to 5 of 5

VBA to compare data in 2 sheets, issues with existing code

Hybrid View

Gti182 VBA to compare data in 2... 05-19-2014, 07:48 AM
AlphaFrog Re: VBA to compare data in 2... 05-19-2014, 09:09 AM
Gti182 Re: VBA to compare data in 2... 05-20-2014, 02:32 AM
AlphaFrog Re: VBA to compare data in 2... 05-20-2014, 08:38 AM
Gti182 Re: VBA to compare data in 2... 05-20-2014, 08:42 AM
  1. #1
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    VBA to compare data in 2 sheets, issues with existing code

    Hi All,

    Found some code from an old post (http://www.excelforum.com/excel-prog...ml#post3701361) which compares data in sheet1 & sheet2 and highlights differences in both sheets.

    Applied below code to my sample file (attached) but picked up some limitations which I was hoping someone could help me with:
    - Code seems to stop comparing any further once it reaches a blank cell
    - Compares dates and values relatively well but not text columns like people's names, department names, etc.
    - There also seems to be some inconsistency in comparing which i can't quite figure out

    Any help would be greatly appreciated.

    Sub Compare()
    
    Dim w1 As Worksheet, w2 As Worksheet
    Dim A1 As Range, A2 As Range
    Dim r1 As Long, r2 As Long
    
    Set w1 = Sheets("Sheet1"): Set w2 = Sheets("Sheet2")
    
    r1 = w1.Range("A" & Rows.Count).End(xlUp).Row
    r2 = w2.Range("A" & Rows.Count).End(xlUp).Row
    
    Sheet1:
    For Each A1 In w1.Range("A2:Q" & r1)
    If A1 = "" Then GoTo Sheet2
    For Each A2 In w2.Range("A2:Q" & r2)
    If A1 = A2 Then GoTo GetNext
    Next
    A1.Interior.Color = vbYellow
    GetNext: Next
    
    Sheet2:
    For Each A2 In w2.Range("A2:Q" & r2)
    If A2 = "" Then Exit Sub
    For Each A1 In w1.Range("A2:Q" & r1)
    If A1 = A2 Then GoTo GetAnother
    Next
    A2.Interior.Color = vbYellow
    GetAnother: Next
    End Sub
    Attached Files Attached Files
    Last edited by Gti182; 05-20-2014 at 02:34 AM.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA to compare data in 2 sheets, issues with existing code

    Try this...

    Sub Compare()
        
        Dim w1 As Worksheet, w2 As Worksheet
        Dim v1 As Variant, v2 As Variant
        Dim i As Long, j As Long, k As Long
        
        Set w1 = Sheets("Sheet1"): Set w2 = Sheets("Sheet2")
        
        v1 = w1.Range("A1").CurrentRegion.Value
        v2 = w2.Range("A1").CurrentRegion.Value
        
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            For i = 2 To UBound(v1, 1)
                .Item(v1(i, 1)) = i
            Next i
            
            Application.ScreenUpdating = False
            
            For i = 2 To UBound(v2, 1)
                If .Exists(v2(i, 1)) Then
                    k = .Item(v2(i, 1))
                    .Item(v2(i, 1)) = 0
                    For j = 2 To UBound(v2, 2)
                        If v1(k, j) <> v2(i, j) Then
                            w1.Cells(k, j).Interior.Color = vbYellow
                            w2.Cells(i, j).Interior.Color = vbYellow
                        End If
                    Next j
                Else
                    w2.Rows(i).Resize(, UBound(v2, 2)).Interior.Color = vbYellow 'No Ref match
                End If
            Next i
            
            For Each v In .Items
                If v > 0 Then
                    w1.Rows(v).Resize(, UBound(v1, 2)).Interior.Color = vbYellow 'No Ref match
                End If
            Next
            
            Application.ScreenUpdating = True
            
        End With
        
    End Sub
    
    
    Sub Clear()
        
        Sheets(1).Cells.Interior.ColorIndex = xlNone
        Sheets(2).Cells.Interior.ColorIndex = xlNone
        
    End Sub
    Last edited by AlphaFrog; 05-19-2014 at 09:30 AM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA to compare data in 2 sheets, issues with existing code

    Alphafrog that is an amazing bit of code. So tidy and efficient works perfectly, thanks so much.

    Do you mind commenting what it does exactly, e.g. what exactly is the Ubound and scripting dictionary function?

    many thanks

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,651

    Re: VBA to compare data in 2 sheets, issues with existing code

    You're welcome.

    In the VBA Editor, highlight the UBound term and press the F1 key for a description.

    The Scripting.Dictionary doesn't have VBA help. It's a special array that has a very fast method to compare Records. It's used in this case to store the 1st sheet ID's (Refs) along with their row numbers (i). Then the 2nd sheet's ID's are compared to the Dictionary to find matches. Matched rows (i and k) are then compared cell by cell (j = columns).

    Sub Compare_Records()
        
        ' Compares records (rows) from two worksheets based on matched record IDs
        
        ' For each macthed record pair, their cells (columns) are compared.
        ' Differentiated cells are highlightd in yellow on both sheets.
        ' It assumes the two sheets have identical data column configurations
        ' Unmatched record IDs on both sheets have their entire row highlighted.
        
        ' Adjust the worksheet references (w1 and w2)
        ' and the column number (colID) that contains the record IDs to suit.
        
        Dim w1 As Worksheet, w2 As Worksheet
        Dim v1 As Variant, v2 As Variant, v As Variant
        Dim i As Long, j As Long, k As Long, colID As Long
        
        Set w1 = Sheets("Sheet1")   'Master records sheet
        Set w2 = Sheets("Sheet2")   'Comparison records sheet
        
        v1 = w1.Range("A1").CurrentRegion.Value 'Master records data
        v2 = w2.Range("A1").CurrentRegion.Value 'Comparison records data
        
        colID = 1   'Column number that contains the record IDs to match between the two sheets; column A = 1
        
        With CreateObject("Scripting.Dictionary")
            .CompareMode = 1
            'Store the Master sheet record IDs with row numbers
            For i = 2 To UBound(v1, 1)
                .Item(v1(i, colID)) = i
            Next i
            
            Application.ScreenUpdating = False
            
            For i = 2 To UBound(v2, 1)          'Loop through Comparison sheet IDs
                If .Exists(v2(i, colID)) Then   'Find a record ID match if any
                    k = .Item(v2(i, colID))     'Master sheet row number of matched ID
                    .Item(v2(i, colID)) = 0     'Tag record ID as matched
                    For j = LBound(v2, 2) To UBound(v2, 2)  'loop through matched record columns
                        'Highlight differentiated cells on both sheets
                        If v1(k, j) <> v2(i, j) Then
                            w1.Cells(k, j).Interior.Color = vbYellow
                            w2.Cells(i, j).Interior.Color = vbYellow
                        End If
                    Next j
                Else
                    'Highlight rows on Comparison sheet that had no record ID match
                    w2.Rows(i).Resize(, UBound(v2, 2)).Interior.Color = vbYellow 'No ID match
                End If
            Next i
            
            'Highlight rows on Master sheet that had no record ID match
            For Each v In .Items
                If v > 0 Then
                    w1.Rows(v).Resize(, UBound(v1, 2)).Interior.Color = vbYellow 'No ID match
                End If
            Next
            
            Application.ScreenUpdating = True
            
        End With
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    10-04-2010
    Location
    London
    MS-Off Ver
    Office 365
    Posts
    469

    Re: VBA to compare data in 2 sheets, issues with existing code

    thanks so much AlphaFrog, really appreciate the extra effort learning more and more each day

+ 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. [SOLVED] Code to compare data in 2 sheets and delete duplicates
    By amar05 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-13-2013, 03:24 PM
  2. [SOLVED] Password issues with code that protects all sheets at once
    By Paragoomba in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-10-2013, 12:57 PM
  3. [SOLVED] open a csv and compare the data with my existing worksheet.
    By sheffieldlad in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 01-09-2013, 07:19 AM
  4. COUNTIF Issues count blank cells among existing data
    By chrispulliam in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 05-21-2010, 01:52 AM
  5. Replies: 1
    Last Post: 08-15-2009, 05:52 AM

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