excel - Test or check if sheet exists -
dim wkbkdestination workbook dim destsheet worksheet each thisworksheet in wkbkorigin.worksheets 'this throws subscript out of range if there not sheet in destination 'workbook has same name current sheet in origin workbook. set destsheet = wkbkdestination.worksheets(thisworksheet.name) next
basically loop through sheets in origin workbook set destsheet
in destination workbook sheet same name iterated 1 in origin workbook.
how can test if sheet exists? like:
if wkbkdestination.worksheets(thisworksheet.name)
some folk dislike approach because of "inappropriate" use of error handling, think it's considered acceptable in vba... alternative approach loop though sheets until find match.
function sheetexists(shtname string, optional wb workbook) boolean dim sht worksheet if wb nothing set wb = thisworkbook on error resume next set sht = wb.sheets(shtname) on error goto 0 sheetexists = not sht nothing end function
Comments
Post a Comment