+ Reply to Thread
Results 1 to 17 of 17

Macro to Compare 2 Tables and highlight 3 defined differences

  1. #1
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Macro to Compare 2 Tables and highlight 3 defined differences

    Hi, I am am in need of a macro to compare 2 tables - both in the same workbook, on 2 different sheets.

    Table1 is a refreshable table with data updated daily
    Table2 is a table with only specific rows from table1 that were added to a new sheet previously (Sheet Name = Workload Tracking)

    table 1 starts at row B21 (headers)

    So, when I run the macro, I'd like to be able to highlight anything that has changed in table1 compared to the data that is visible in the columns of in table2. Table 2 will have 1 column (1) that will be in table 2 --- and table 2 will have the first 4 columns that will not be in table1 and should be ignore -- so, column 2 of table1 = column 5 of table2... and all columns after that will be the same.

    3 different forms of highlighting for the differences
    1. Red = If the "FileNo" is no longer on table1 but is on table 2 (Highlight entire table row red)
    2. Green = If the data in table1 is filled in, but the data in table2 is not (data = the specific cell, found by the "FileNumber" row, and the column header name)
    3. Yellow = If the data in table1 is filled in, and the data in table2 is filled in, but there is a difference

    The comparison needs to be by "FileNo" (This is a unique value) and the Column Headers (visible in table2 but all of table 1 are possibilities, depending on which columns the user has hidden in table2) name and the data that is contained in the other, visible, columns

    I have attached a workbook with notes, explaining in more detail as I think that will be clearer

    Table1 actual name in my data = Table_CollPreComplaint and is on worksheet "Dashboard"
    Table2 actual name in my data = Table_WorkloadTracking as is on worksheet "Workload Tracking"

    --- In the example attached - both are named the same as my actual workbook dashboard

    This example is MUCH smaller than the actual workbook, which is around 15,000 rows --- and shows both tables, with example columns hidden -- i hope it clarifies everything but if not, please let me know and ill be happy to clarify!

    Appreciate any help!
    Attached Files Attached Files

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    I only did a very quick QA on this.
    Attached Files Attached Files
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Awesome, I will give it a go this evening! Thank you for taking a look!

  4. #4
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    This works great -- thank you! The one tweak I have, and this is my fault for not specifying, is that I would also need to transfer over the data contained within the cell in the Dashboard Table for fields that are different (so, yellow and green) -- that way, I can see not only what has changed but also the new value

  5. #5
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    So the yellow or green indicates that the value in this cell is new. Do you want to preserve the old value anywhere?

  6. #6
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    I think this includes the new requirement:
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Yessir - Perfect. Thank you!

  8. #8
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Question - What range is this referring to? For jR = 9 To 47

    at fist i assumed columns but that would be strange since it starts at 9..

    also, have been getting a type mismatch error at this line when running.. If shD.Cells(rng.Row, jD) <> "" And shW.Cells(cl.Row, jR) = "" Then

    The only thing I changed is adding an error catch, application false/tru's... and "A" to "B" in this line.. and AU to BC..

    With shW.Range(shW.Cells(cl.Row, "B"), shW.Cells(cl.Row, "BC")).Interior

    Please Login or Register  to view this content.

  9. #9
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    jD is the "working" column on the dashboard sheet and jR is the working column on the Workload tracking sheet. The columns are offset on the sheets. so I start in column 9 on the tracking sheet and that corresponds to column 6 of the dashboard sheet.

    I did a poor job commenting this and also I don't know why I named it jR since it should have been jW to match the sheet it is working on.

    When you get the error on shD.Cells(rng.Row, jD) <> "" And shW.Cells(cl.Row, jR) what are the values of rng.row, jd cl.row and jR? That will tell you the two cells that are being compared. Although that should not be giving a type mismatch error.

    Try
    Please Login or Register  to view this content.

  10. #10
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    ahh, that makes sense --- 10-4 will do tomorrow morning -- thanks!

  11. #11
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Okay, so, using the attached macro (Updated jr to 6 to 71) I am assuming that the column numbers here are the sheet columns and not the table columns..

    Using the original Code line - [If shD.Cells(rng.Row, jD) <> "" And shW.Cells(cl.Row, jR) = "" Then] - the variable values at the time of error are:
    rng.Row = 3660
    jD = 6
    cl.row = 173
    jR = 9

    Using [If Len(shD.Cells(rng.Row, jD)) <> 0 And Len(shW.Cells(cl.Row, jR)) = 0 Then] I get the same error.. with same variable values at time of error
    rng.Row = 3660
    jD = 6
    cl.Row = 173
    jR = 9
    Len(shW.Cells(cl.Row, jR)) = "<"type Mismatch">" --- had to put quotes on this to avoid html block


    rng.Row 3660 and cl.Row 173 are the same FileNo
    Attached Files Attached Files

  12. #12
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    And it works for all other values obviously at least up to that point. What confuses me is that Type Mismatch happens when you have a declared variable type and attempt to assign a value to it that doesn't fit such as declaring the type as long and trying to assign the letter "A" to it.

    That's not what I am doing here. I am comparing the contents of one cell against the contents of another cell. Excel should know that the number 1 is not equal to the letter A and not complain about them being different types.

    As a first test. on a separate sheet, copy / Paste one value into a cell. Then Copy / past the other value into a cell then =Cell1=Cell2 should yield either true or false. If they are the same visually and the result is false then there is an issue. The value that is causing the problem is client.

    As a follow-on test, can you copy / paste the contents of one cell into the other and see what it does with that? Or better still type in the values manually.

    If it runs with the follow-on test (or errors out somewhere else), then there is an issue with the data. If the data is being downloaded from an external system there's a possibility that it may be "dirty." If this is the case, I have some tools to identify where the dirt is.

  13. #13
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Okay, I see what's happening -- If the value on either table = N/A (I have some macros that run and assign a 25 digit ID code to the account to identify what stage its in.. often times, since its 25 digits, the ID hasn't been translated yet since this is a new dashboard. So, I guess what needs to happen is that the code needs to be able to recognize the N/A as a value and then mark it as a non-match

  14. #14
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    I went ahead and added the code to copy the entire table onto itself as values at the end of the macro's and re-ran but then it gave the same error. I copy each cell into another sheet and did the == and it was true. One thing that was different between them is the font and size of font... not sure if that would have an effect though?

  15. #15
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    A couple of things if I need to look at N/A as a non-match I need to know if it is the string "N/A" or the error value #N/A!.

    I understand that you compared the table against itself and got the same error. The font should not be an issue. One possible issue is column width but this should only affect numeric data. I recently discovered an interesting quirk in Excel. If a column contains 12345 but is only wide enough to display the "12" it will display it as scientific notation 12 E3. even though the number is the same internally, it fails to match 12345 because it looks different on the display. The fix for this issue was to make the column wide enough to display the number.

    You could try formatting the two cells the same to see if that works. Let me know if it does.

    How sensitive is the data? Can you post with actual values?

  16. #16
    Forum Contributor
    Join Date
    08-08-2015
    Location
    North Carolina USA
    MS-Off Ver
    MS Office 2016
    Posts
    368

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Let me see which values are giving the problem and then ill see if they are the fields that are sensitive - if not, I can change the sensitive ones and send. The string is #n/a! --- I am going to try and see about adding "if error" to the vlookup thats happening... the vlookup is based on a 25 digit string of 1s and 0's from 25 date columns --- being populated, or not --- which is a DAX calculated field in powerpivot. Will get back to you in a couple hours, thanks!
    Last edited by BG1983; 02-06-2017 at 09:24 PM.

  17. #17
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,920

    Re: Macro to Compare 2 Tables and highlight 3 defined differences

    Yes, IFERROR will help clean up the data a lot. 'Tis better to clean it up on the excel side rather than in code. In fact, according to my philosophy, it's better to let excel do as much of the work as possible and use VB to blot and sew the pieces together.

+ 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: 11-27-2016, 05:56 PM
  2. [SOLVED] VBA to compare and highlight differences
    By REFisher14 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-19-2014, 03:18 PM
  3. [SOLVED] Compare Two Workbooks and Highlight Differences
    By mab1284 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-07-2014, 07:35 PM
  4. [SOLVED] Compare rows and highlight differences
    By cheeze83 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-29-2013, 04:38 AM
  5. Replies: 9
    Last Post: 04-18-2013, 03:54 PM
  6. Macro to compare two excel files and highlight the differences
    By neets1228 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-14-2012, 05:18 AM
  7. Compare Two Worksheets:highlight the differences
    By Jim in forum Excel General
    Replies: 1
    Last Post: 10-11-2005, 04:05 PM

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