vba - Comparing 2 excel workbooks and highlighting the differences -
i'm trying create macro compare 2 excel work books , highlight differences. i've looked previous solutions , helped lot, still can't macro running. i've created 2 scripts see 1 works best.
1.
sub compareworkbooks() dim varsheeta variant dim varsheetb variant dim strrangetocheck string dim irow long dim icol long strrangetocheck = "a1:ak900" 'get worksheets workbooks set wbkn = workbooks.open(filename:="u:\gebouwensep.xlsx") set nieuweversie = wbkn.worksheets("gebouwen") set wbko = workbooks.open(filename:="u:\gebouwenaug.xlsx") set oudeversie = wbko.worksheets("gebouwen") if nieuweversie <> oudeversie nieuweversie.sheets(gebouwen).cells(irow, icol).interior.color = vbyellow end if end sub
2
sub compareworkbooks() dim varsheeta variant dim varsheetb variant dim strrangetocheck string dim irow long dim icol long dim mycell range dim mydiffs integer nlin = 1 ncol = 1 'get worksheets workbooks set wbka = workbooks.open(filename:="u:\gebouwensep.xlsx") set varsheeta = wbka.worksheets("gebouwen") set wbkb = workbooks.open(filename:="u:\gebouwenaug.xlsx") set varsheetb = wbkb.worksheets("gebouwen") strrangetocheck = "a1:ak900" varsheeta = varsheeta.range(strrangetocheck) varsheetb = varsheetb.range(strrangetocheck) irow = lbound(varsheeta, 1) ubound(varsheeta, 1) icol = lbound(varsheeta, 2) ubound(varsheeta, 2) if not varsheeta(irow, icol) = varsheetb(irow, icol) else nieuweversie.sheets(gebouwen).cells(irow, icol).interior.color = vbyellow end if next icol next irow end sub
my solution code 2
your main problem appears following line:
nieuweversie.sheets(gebouwen).cells(irow, icol).interior.color = vbyellow
you don't have variables set "nieuweversie" or "gebouwen". former appears remnant in code 2 left on code 1, i'm assuming need line is:
wbka.sheets("gebouwen").cells(irow, icol).interior.color = vbyellow
i've tested solution myself , appears fixing worksheet reference causes sub run correctly
also - isn't working in code 1 either due same line (i haven't checked rest of code though, there may other issues). variable "nieuwerversie" worksheet, , you're doing referring "worksheet.sheets" object, not valid reference. if used
nieuweversie.cells(irow, icol).interior.color = vbyellow
then should fix same line in code 1, although haven't tested rest of code
Comments
Post a Comment