+ Reply to Thread
Results 1 to 13 of 13

Comparing 2 Rows By Looping Through Cells

  1. #1
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Comparing 2 Rows By Looping Through Cells

    I've uploaded my code with an example database.

    The macro is supposed to generate a report on what has changed between the "Current" and "Previous" sheets.

    The first step is to compare Current to Previous by the reference column and determine if there were any new records added. If so those records are copied to the "Report Comparison" sheet and highlighted yellow. This seems to be working.

    The second step is to compare Previous to Current and determine if there were any records that were deleted. If so those are copied to "Report Comparison" and highlighted green. This seems to be working also.

    Next the macro is to compare each record cell by cell, and if there is a particular cell that has been changed in a record, the entire row from Current is copied to "Report Comparison", but only the cell that changed is highlighted yellow and a comment is added to that cell showing what the previous value was( adding the comment has not been attempted in the code yet).

    The last section of the code is commented out for now, but I had started by looping the cells in one specific row to see if the code was correct before trying to also loop through all the rows. If I include this code the results aren't correct. All the rows from Current and Previous are getting copied to the report. I'm not sure where the code is wrong.

    Any help is appreciated. Thanks
    Attached Files Attached Files

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

    Re: Comparing 2 Rows By Looping Through Cells

    Try this...

    Please Login or Register  to view this content.
    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
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Re: Comparing 2 Rows By Looping Through Cells

    AlphaFrog,

    Thanks for the help. Your code is a lot more efficient than mine. I'm getting a run time error, I've attached a screenshot.

    Quote Originally Posted by AlphaFrog View Post
    Try this...

    Please Login or Register  to view this content.
    Attached Images Attached Images

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

    Re: Comparing 2 Rows By Looping Through Cells

    What's the error description?
    Did you test it with your "DBCompare - Example.xlsm" workbook.?
    Last edited by AlphaFrog; 08-21-2015 at 01:09 PM.

  5. #5
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Re: Comparing 2 Rows By Looping Through Cells

    Quote Originally Posted by AlphaFrog View Post
    What's the error description?
    Did you test it with your "DBCompare - Example.xlsm" workbook.?
    I'm getting "Run Time Error 1004 Application-defined or object-defined error"

    I copied your code into the same workbook I uploaded and created a new macro DatabaseCompare2.

  6. #6
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Re: Comparing 2 Rows By Looping Through Cells

    Quote Originally Posted by AlphaFrog View Post
    What's the error description?
    Did you test it with your "DBCompare - Example.xlsm" workbook.?
    I've attached the workbook with the results of the error. The last cell in the last row is where the error is, the code is attempting to add a comment.
    Attached Files Attached Files

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

    Re: Comparing 2 Rows By Looping Through Cells

    I can't get it to error.

    When it errors for you, what are the values of...
    NextRow
    c
    v
    Cells(NextRow, c)
    wsPrev.Cells(v, c).Value

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

    Re: Comparing 2 Rows By Looping Through Cells

    I got it to error.

    Change this...
    Cells(NextRow, c).AddComment.Text wsPrev.Cells(v, c).Text 'show change as comment

  9. #9
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Thumbs up Re: Comparing 2 Rows By Looping Through Cells

    Quote Originally Posted by AlphaFrog View Post
    I got it to error.

    Change this...
    Cells(NextRow, c).AddComment.Text wsPrev.Cells(v, c).Text 'show change as comment
    Yep that worked. I really appreciate it.

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

    Re: Comparing 2 Rows By Looping Through Cells

    You're welcome.

  11. #11
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Re: Comparing 2 Rows By Looping Through Cells

    Quote Originally Posted by AlphaFrog View Post
    Try this...

    Please Login or Register  to view this content.
    AlphaFrog,

    I took your code and tried to replace specific row/column numbers to variables since this macro can be used with a variety of databases. I'm a bit stuck at this point where if I run the macro, it acts like it doesn't know which sheet to copy the data to, so the "Current" sheet gets messed up. I've attached the workbook with the aftermath of "Current". I made a backup of the "Current" sheet in order to test the macro again.

    Thanks
    Attached Files Attached Files

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

    Re: Comparing 2 Rows By Looping Through Cells

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-19-2015
    Location
    Houston, TX
    MS-Off Ver
    Pro 2013
    Posts
    30

    Thumbs up Re: Comparing 2 Rows By Looping Through Cells

    Works like a charm. Thanks again.

+ 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. Replies: 2
    Last Post: 06-21-2015, 07:57 AM
  2. [SOLVED] Looping through and deleting rows if cells contain a value
    By martinez77 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-08-2014, 01:02 PM
  3. Comparing Two Rows And Highlighting Different Cells
    By ExcelNewbieSK in forum Excel General
    Replies: 6
    Last Post: 01-18-2013, 03:13 PM
  4. Looping though range and comparing to current cell
    By Aussiexile in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-04-2010, 02:30 AM
  5. Comparing 2 cells and copying rows to new worksheet
    By craig456 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-06-2006, 01:09 PM
  6. Looping through rows in range AND comparing range cells
    By Damask in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-16-2006, 10:30 PM
  7. Comparing two cells in a colum with 2000 rows
    By thomas1075 in forum Excel General
    Replies: 3
    Last Post: 06-07-2005, 03:12 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