Compare two data sets in different sheet and print difference in other sheet using excel VBA? -
i have 2 data sets in 2 different sheet sheet1 orginal ref , sheet2 comparison. sheet2 data should compared sheet1 , print entire mismatched row of sheet2 , highlight cells has mismatch data , difference should printed column header in other specified sheet , in specified range
also mismatch cell count should updated in sheet3 specified range cell
below tried code. appreciated.
sub comparedataset() call comparesheets("sheet1", "sheet2") end sub sub comparesheets(sheet1 string, sheet2 string) dim cell range dim cellmismatch integer each cell in activeworkbook.worksheets(sheet1).usedrange if not cell.value = activeworkbook.worksheets(sheet2).cells(cell.row, cell.column).value let worksheets("sheet3").cells(cell.row, cell.column) = cell cell.interior.color = vbyellow cellmismatch = cellmismatch + 1 end if next thisworkbook.sheets("sheet3").cells(1, 1).value = cellmismatch end sub
here code compare sheet1 , sheet2(corresponding cells ) , according wite correct value or mismatch based upon result sheet3. sheet1 , sheet2 wil have same number of rows , columns , headers same can keep them in sheet3. hope helps.
sub compare() 'clearing contents of third sheet fresh comparison usedcoulms = sheets("sheet3").usedrange.columns.count usedrows = sheets("sheet3").usedrange.rows.count = 2 usedrows j = 1 usedcoulms sheets("sheet3").cells(i, j).value = "" sheets("sheet3").cells(i, j).interior.color = rgb(255, 255, 255) next next 'coulmn count of first sheet columncount = sheets("sheet1").usedrange.columns.count 'row count of first sheet rowcount = sheets("sheet1").usedrange.rows.count = 2 rowcount j = 1 columncount if sheets("sheet1").cells(i, j).value <> sheets("sheet2").cells(i, j).value 'comparing if values not equal sheets("sheet3").cells(1, j).value = sheets("sheet1").cells(1, j).value 'copying header of mismatched cell sheets("sheet3").cells(i, j).value = cstr("mismatch") 'if mismatch setting set value mismatch sheets("sheet3").cells(i, j).interior.color = 65535 'highlighting yellow color else sheets("sheet3").cells(i, j).value = sheets("sheet1").cells(i, j).value 'if values same copy first sheets value if dont want copy can skip end if next next end sub
Comments
Post a Comment