+ Reply to Thread
Results 1 to 8 of 8

VBA Code compare two spread sheets and highlight difference errors out on no match

Hybrid View

  1. #1
    Registered User
    Join Date
    07-20-2012
    Location
    us
    MS-Off Ver
    Excel 2003
    Posts
    42

    VBA Code compare two spread sheets and highlight difference errors out on no match

    I need to compare two spread sheets that have the same employee data structure, but the data in each cell might be in a different and the list will not be in the same order each week with new hires and separations. Each week I upload data in a file and I want to compare last weeks data with the new weeks data and highlight the differences on the new report. The code I have below does this exactly how I want it to, but there is one issue. It errors out when it comes to a new employee that wasn't on the previous weeks report. I understand why it errors out, because the match function doesn't find a match. But I am not sure how I can get around this error? I have tried adding a error handler, but I couldn't get it to work right. I tired using on error resume next, but that creates another issue.

    Below is the code I created. My end game would be to highlight the cells that had data changes in yellow (which it current does) and highlight new employees row in red. If possible, I would also like to be able to identify from the old report who is not on the new report and copy that row of data to the new report and highlight a difference color to easily identify who was removed.

    Thank You for taking the time to help.

    Sub Compare()
    
    Dim LR As Integer
    Dim I As Integer
    Dim R As Long
    Dim C As Integer
    
    'On Error GoTo ErrorHandler
    
    LR = Worksheets("New").Range("A20000").End(xlUp).Row
    
    For I = 1 To LR
    
               R = Application.WorksheetFunction.Match(Worksheets("New").Cells(I, 1).Value, Worksheets("Previous").Range("A:A"), 0)
        
               For C = 1 To 5
        
                   If Not Worksheets("New").Cells(I, C).Value = Worksheets("Previous").Cells(R, C).Value Then
            
                   Worksheets("New").Cells(I, C).Interior.Color = vbYellow
            
                   End If
        
               Next C
    1:
      Next I
       
        
      'ErrorHandler:
    
    'Worksheets("New").Cells(I, 1).Interior.Color = vbRed
     
    'GoTo 1:
    
    End Sub

  2. #2
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    You cant do that this way.
    I think that "On Error Resume Next" should have worked here.
    Sub Compare()
    
    Dim LR As Integer
    Dim I As Integer
    Dim R As Long
    Dim C As Integer
    
    
    LR = Worksheets("New").Range("A20000").End(xlUp).Row
    
    For I = 1 To LR
    
    On Error Resume Next
    
               R = Application.WorksheetFunction.Match(Worksheets("New").Cells(I, 1).Value, Worksheets("Previous").Range("A:A"), 0)
        
               For C = 1 To 5
        
                   If Not Worksheets("New").Cells(I, C).Value = Worksheets("Previous").Cells(R, C).Value Then
            
                   Worksheets("New").Cells(I, C).Interior.Color = vbYellow
            
                   End If
        
               Next C
    
    Next I
    
    End Sub
    Still I am not sure.
    Do tell me if this fails.
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "I don't get things easily, so please be precise and elaborate"

    If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
    If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.

    Sourabh

  3. #3
    Registered User
    Join Date
    07-20-2012
    Location
    us
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    Adding the on error resume next to that location does not fail, but it does not do what I want. When it resumes next, it does not update R to the new row number which then causes it to highlight cells that did not change. Below screenshot shows what I am talking about. In column A, number 8 (row 3), number 9 (row 4) and number 7 (row7) were not on the previous data. You can see it highlighted the data in column C on these rows. It did this because when it error out, it continues to compare the last row it was on.

    example.PNG

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    Hi, anfdrew,

    change the dimension for R from Long to Variant and add an If for checking if any error exists like
    Sub Compare()
    
    Dim LR As Long
    Dim I As Long
    Dim R As Variant
    Dim C As Long
    
    LR = Worksheets("New").Range("A20000").End(xlUp).Row
    
    For I = 1 To LR
      R = Application.Match(Worksheets("New").Cells(I, 1).Value, Worksheets("Previous").Range("A:A"), 0)
      If IsErr(R) Then
        For C = 1 To 5
          If Not Worksheets("New").Cells(I, C).Value = Worksheets("Previous").Cells(R, C).Value Then
           Worksheets("New").Cells(I, C).Interior.Color = vbYellow
          End If
        Next C
      End If
    Next I
    
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  5. #5
    Registered User
    Join Date
    07-20-2012
    Location
    us
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    Holger,

    I get a compile error. Sub or Function not defined and it highlights the IsErr.

  6. #6
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    @ hahobe
    I don't know much about VBA, but why did you use IsErr, I think it ignores #N/A error. So should it not be #IsError
    @ anfdrew,

    Why are you comparing the first column? You are matching the values of first columns in both the sheets. They why are you comparing the values?
    Also instead of worksheetfunction just use application.match.
    It has better ability to deal with errors.
    Use this macro-

    Sub Compare()
    
    Dim LR As Integer
    Dim I As Integer
    Dim R As Variant
    Dim C As Integer
    
    
    LR = Sheets("New").Range("A20000").End(xlUp).Row
    LR1 = Sheets("Previous").Range("A20000").End(xlUp).Row
    For I = 1 To LR
    R = Application.Match(Sheets("New").Cells(I, 1).Value, Sheets("Previous").Range("A1:A" & LR1), 0)
       
            If IsError(R) Then
            Worksheets("New").Cells(I, 1).Interior.Color = vbRed
            Else
            
               For C = 2 To 5
        
                   If Not Worksheets("New").Cells(I, C).Value = Worksheets("Previous").Cells(R, C).Value Then
            
                   Worksheets("New").Cells(I, C).Interior.Color = vbYellow
            
                   End If
        
               Next C
        End If
    
    Next I
    
    End Sub
    Check Attached.
    Attached Files Attached Files

  7. #7
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    Hi, sourabhg98,

    the command should have been IsError indeed - my head must have been on the way to work while my fingers were still typing and beong called to sped up.

    Ciao,
    Holger

  8. #8
    Registered User
    Join Date
    04-28-2015
    Location
    bangalore
    MS-Off Ver
    2013
    Posts
    56

    Re: VBA Code compare two spread sheets and highlight difference errors out on no match

    Hi Sourabh,

    I was watching threads in this forum.. This is the great help before I posted thread, I was checking that is there any examples available in this forum.

    Now I got it . I need help, In the attached sheet , instead of using colors, can u pls help to create new sheet only for changes and new added.

    Thanks
    Kumar PK

+ 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] VBA code to compare rows based on 4 column values and highlight is no match
    By Sirilias in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-16-2015, 02:22 PM
  2. vba code to compare coloumns on two worksheet and highlight difference
    By Sragel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-06-2015, 09:44 PM
  3. vba macros to compare 2 worksheets and highlight the difference
    By hecgroups in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-13-2015, 11:16 AM
  4. Compare Two Columns and Highlight the Difference
    By VickyVykciV in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-25-2014, 01:21 AM
  5. Macro that can compare sheets between 2 workbooks, highlight the difference
    By jeffboy29 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-19-2013, 11:31 AM
  6. Replies: 2
    Last Post: 04-12-2013, 06:20 PM
  7. [SOLVED] RE: How do you compare 2 list of numbers and highlight the difference
    By Tom Ogilvy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-20-2006, 10:55 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