excel - Check sheet names to avoid duplicates -
i working on macro, part of takes input user asking he/she rename sheet. works fine, run runtime error if name provided user being used different sheet. understand why error occurs not sure how warn user , handle error.
my code follows:-
'change sheet name dim sheetname string sheetname = inputbox(prompt:="enter model code (eg 2sv)", _ title:="model code", default:="model code here") wscopyto.name = sheetname
there 2 ways handle this.
first, trap error, check if there error, , advise, put error trapping was
dim sheetname string sheetname = inputbox(prompt:="enter model code (eg 2sv)", _ title:="model code", default:="model code here") on error resume next err.clear 'ensure unhandled errors not give false positive on err.number wscopyto.name = sheetname if err.number = ?? 'go , ask name on error goto 0
second, check current sheet names, , see if there match
dim sheetname string dim sh 'as sheet sheetname = inputbox(prompt:="enter model code (eg 2sv)", _ title:="model code", default:="model code here") each sh in activeworkbook.sheets if lower(sh.name)=lower(sheetname) 'goback , ask name next wscopyto.name = sheetname
Comments
Post a Comment