+ Reply to Thread
Results 1 to 15 of 15

comparison macro

  1. #1
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    comparison macro

    Hi All,
    Can anyone help me here please....
    I need to compare the values of data on one sheet (sheet1) with that of another (sheet2) to see if there has been any changes between the sheets over the previous week. If a macro could go through my data on sheet1, compare cell-by-cell the data that's in sheet 2 and highlight in red font any cells that have different values that would be awesome.
    The macro would need to leave the "fill color" of the cell as it was.

    Any help much appreciated

    Thanks

    Beatrice
    Last edited by VBA Noob; 10-29-2008 at 03:10 PM.

  2. #2
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    I'm assuming by "cell-by-cell comparison" you mean Sheet1 A1 vs Sheet2 A1.

    The following will compare the first two worksheet in the workbook and highlight any cell on the second sheet whose value do not match the corresponding on the first sheet.

    Change the number in the worksheets(1) to either the sheet number or the sheet name such as worksheets("sheet1")

    Please Login or Register  to view this content.

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Perhaps this will do what you want. It sets CF on the cells, rather than looping and changing the cell color.
    Please Login or Register  to view this content.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    works well but....

    Hi,
    these macros seem to work very well so thank you so much. One problem I now have found is that some cells have conditional formatting on them which over-rides the font color change. Would it be possible to include in the macros for the border colors to be changed to red too for the cells with different values as well as the font (which it already changes). Note that the cell fill color has to be left as it is.

    Thanks heaps

    B.

  5. #5
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    Using the non-conditional formatting method:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    Thanks mdbct

    Hi mdbct,
    thanks for the code,
    I was playing around with the code yesterday and tried just doing this (copying colorindex line) and it seems to work! What would the downside of this be?Also how do I change the sheet name on this code? to eg shet 1 = max, sheet 2 = mine?

    Thanks

    B

    Please Login or Register  to view this content.
    Last edited by beatrice25; 10-28-2008 at 06:20 PM.

  7. #7
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    From my first post
    Change the number in the worksheets(1) to either the sheet number or the sheet name such as worksheets("max")
    (altered slightly to use the info you've provided).

    I'm not sure I know what you mean
    tried just doing this (copying colorindex line) and it seems to work! What would the downside of this be?
    Your method of using the Border.ColorIndex is much more compact (I used the macro recorder and got the bloated method. I should have thought about it more instead of taking the "easy way")

  8. #8
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    fantastic..can this macro be added to another?

    Thanks, that was a bit of a numb skull question which I shot off before thinking about.

    Thanks for all your help, the macro works very well and now gets rid of discrepancies that have been causing problems in the past.

    Is it possible to add this macro to work at the end of a previous macro action
    ie. I now have a macro that I recorded to copy and past a sheet then take out some columns before I run the macro that you wrote, can these two be easily combined? The only way i can do it currently is to add some recorded code that selects your macro and runs it, e.g.
    Application.Run "'Milestone281008v2.xls'!Highlight"
    but this stops working as soon as I change the workbook (Milestone281008v2.xls) name.

    Here is what I run before your one:
    Please Login or Register  to view this content.
    Thanks again mdbct!

    B.
    Last edited by beatrice25; 10-28-2008 at 06:21 PM.

  9. #9
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    Please wrap the code in your last two posts.

    You can shorten the column deletion code. There is no need to select the columns. Here is a quick example to delete colUmns A, B, F, G, H, P, and q:
    Please Login or Register  to view this content.
    You should be able to paste my macro to the end of the one you posted. and have it run that way. Remove the End Sub at the end of your macro and the Sub Name() from the beginning of mine.

  10. #10
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    work well thanks

    It's only when one runs one's macro does one see the flaws in it. I found that In need to not only highlight if the values are different between sheet 1 and sheet 2 but also if the interior color is different.
    I tried adding the line
    Please Login or Register  to view this content.
    and added an additional end if, the macro still runs, takes longer to run but doesn't actually highlight a cell where the value on (e.g.) sheet1 cell a1 = the value of sheet2 cell a1 but where the fill color of the cell differs. I would have thought my addition would have done it ...but it doesn't seem to!

  11. #11
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    You were close. You told the macro to check the interior color against the value of the cell. You can use an or statement since either the interior color or the value being different cause a "true"for your if statement.


    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    now getting compile error

    Hi Mdbct,
    Thanks for your code, I added it to the existing code and commented out the previous IF statement, however the new code instantly went red and came up with compiling error, do you know what may be causing this?


    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor mdbct's Avatar
    Join Date
    11-11-2005
    Location
    CT
    MS-Off Ver
    2003 & 2007
    Posts
    848
    It had to do with the wrapping for the "If Or" statement we added.

    When wrapping lines in VBA the method is a space, an underscore and then enter. or

    Try this version

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    perfect....just perfect!

    thank you sooooo much, about 10 other people in our engineering group will use this macro and it will totally change our tracking, we will no longer have version management problems as his macro will capture any changes that people make.



    B

  15. #15
    Registered User
    Join Date
    05-16-2006
    Posts
    93

    Smile my problem is resolved - thank you mdbct

    my problem is resolved - thank you mdbct

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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