compare two different workbooks and highlight differences excel vba -
so trying compare 2 different workbooks in excel vba. know application "compare spreadsheets" exist need general code. have looked lot in forum trying find solution no 1 match needs.
what have: script that: askes user excelfile (file2) compare current file(file1). differences need highlighted in current file.
i kind of beginner vba please gentle :) have code trying use works comparison of 2 sheets in same workbook.
if got code job or me step further kind share code. or if have tips on how can make code down below match needs. alot!!
`sub test() dim varsheeta variant dim varsheetb variant dim strrangetocheck string dim irow long dim icol long strrangetocheck = "a1:iv65536" ' if know data in smaller range, reduce size of ranges above. debug.print varsheeta = worksheets("sheet1").range(strrangetocheck) varsheetb = worksheets("sheet 1").range(strrangetocheck) ' or whatever other sheet is. debug.print irow = lbound(varsheeta, 1) ubound(varsheeta, 1) icol = lbound(varsheeta, 2) ubound(varsheeta, 2) if varsheeta(irow, icol) = varsheetb(irow, icol) 'msgbox ("no difference") ' cells identical. ' nothing. else highlightcells ' cells different. ' code goes here whatever want do. end if next icol next irow
end sub ` modifications have tried do, make section(s) different bold or prompt user:
sub test()
dim varsheeta variant dim varsheetb variant dim strrangetocheck string dim irow long dim icol long strrangetocheck = "a1:n70" ' if know data in smaller range, reduce size of ranges above. debug.print varsheeta = worksheets("229019_pss360_17w15").range(strrangetocheck) varsheetb = worksheets("229019_pss360_ny").range(strrangetocheck) ' or whatever other sheet is. debug.print irow = lbound(varsheeta, 1) ubound(varsheeta, 1) icol = lbound(varsheeta, 2) ubound(varsheeta, 2) if varsheeta(irow, icol) <> varsheetb(irow, icol) selection.font.bold = true 'msgbox ("diff") ' cells identical. ' nothing. else 'msgbox ("same") ' cells different. ' code goes here whatever want do. end if next icol next irow
end sub
use code prompt file opening , open selected file:
private sub open_file(length integer) dim strpath string dim temp_workbook workbook dim temp_worksheet worksheet application.filedialog(msofiledialogopen).allowmultiselect = false 'make file dialog visible user intchoice = application.filedialog(msofiledialogopen).show 'determine choice user made if intchoice <> 0 'get file path selected user strpath = application.filedialog(msofiledialogopen).selecteditems(1) createobject("scripting.filesystemobject") strext = .getextensionname(strpath) end if strcomp(strext, "xlsx", vbtextcompare) = 0 or strcomp(strext, "xlsm", vbtextcompare) = 0 or strcomp(strext, "xls", vbtextcompare) = 0 else ' display error msgbox ("wrong file format. accepted formats .xlsx , .xlsm") exit sub end if end if if intchoice <> -1 if msgbox("cancel operation?", vbyesno, "confirmation") = vbyes exit sub else open_file (length) end if end if set temp_workbook = workbooks.open(strpath, true, true) set temp_worksheet = sheets(1) end sub
at end of sub, have asked user select file, , have opened said file (making sure excel file beforehand). @ point want copy data temp_worksheet
variant
, compare values the values source sheet (the 1 comparing with) have stocked in variant array
:
dim strarray() variant dim strarray2() variant strarray = sourcesheet.range(yourrangehere) strarray2 = temp_worksheet.range(yoursecondrangehere)
at point need loop through arrays , compare values accordingly. once difference spotted, can highlight the difference in corresponding sheet. i'll let figure last part out it's straightforward. if need see how data stored in strarray
, strarray2
define breakpoint in code , run add spy (select strarray
, right click , "add spy") see values in real time. luck!
Comments
Post a Comment