Compare Cells in Excel Sheets and mark differences with colors...
Just got a task that there is a database table which got row add / delete / updates and wants to compare and mark out differences in Excel sheets.
Input:
- Original Database Table named "orgTable"
- Updated Database Table named "rstTable"
- orgTable and rstTable are the same tables that have same schema
Output:
- Excel file with sheets of
- Added rows
- Deleted rows
- Primary-key mapped orgTable rows with cell-color marked differences from rstTable
- Primary-key mapped rstTable rows with cell-color marked differences from orgTable
Steps:
- Prepare Table-View SQL Scripts
- Added rows view
- select * from rstTable where rstTable.primaryKey not in (select orgTable.primaryKey from orgTable) order by rstTable.primaryKey
- Deleted rows view
- select * from orgTable where orgTable.primaryKey not in (select rstTable.primaryKey from rstTable) order by orgTable.primaryKey
- Primary-key mapped orgTable rows view
- select * from orgTable where orgTable.primaryKey in (select rstTable.primaryKey from rstTable) order by orgTable.primaryKey
- Primary-key mapped rstTable rows view
- select * from rstTable where rstTable.primaryKey in (select orgTable.primaryKey from orgTable) order by rstTable.primaryKey
- Added rows view
- Export Views to Excel Sheets
- Using SQL Server Data Export function to export those views to a single Excel file with multiple sheets.
- Using Excel VBA Macro to mark colors
- Open exported Excel file
- Open VBA Macro Editor
- Identify orgTable and rstTable mapped output sheets name (here defining "orgTableSheet" as Sheet3 object and "rstTableSheet" as Sheet4 object)
- Create the following CompareSheet function by adding a new Module:
Sub comparesheet()
'''''using rstTableSheet (Sheet4) as base for compare
For Each MyCell In Sheet4.UsedRange
'''''first reset the cell with white background
MyCell.Interior.ColorIndex = 0
Sheet3.Range(MyCell.Address).Interior.ColorIndex = 0
'''''if cell is not empty... If Trim(MyCell.Value) <> "" Then
'''''if cells in the same position of those 2 sheets got different values...
If Trim(MyCell.Value) <> Trim(Sheet3.Range(MyCell.Address).Value) Then
'''''paint both cells background to red MyCell.Interior.ColorIndex = 3
Sheet3.Range(MyCell.Address).Interior.ColorIndex = 3
End If
End If
Next
End Sub
- Run the macro to mark colors in both sheets.
FYI.
Let me know if you got more efficient ways to do this task...
Technorati tags: microsoft, office, excel, macro, VBA, tip
Comments
Anonymous
September 28, 2007
PingBack from http://www.artofbam.com/wordpress/?p=3615Anonymous
July 16, 2008
I'm looking for something similar, except that I want to transfer color coding from 1st sheet to updated sheet. I tried practicing with this code but get error at "For Each MyCell In Sheet4.UsedRange".Anonymous
May 31, 2009
PingBack from http://outdoorceilingfansite.info/story.php?id=20801Anonymous
June 13, 2009
PingBack from http://outdoordecoration.info/story.php?id=3212Anonymous
October 13, 2010
Hi did you ever get something which works - I am looking for exactly the same thing and would be grateful for your help Regards