excel - Copying existing password protected sheet to new workbook as an unprotected sheet does not make the new worksheet unprotected -
copying existing password protected sheet new workbook unprotected sheet gives following error when user tries type in data in new worksheet.
error: "the cell or chart you're trying change on protected sheet"
click ok on error message.
please note error happens once. click ok on pop error message , type again, excel allows type data in cells , save sheet.
we have excel (format .xls) file being used create excel spreadsheet when button on form in same spreadsheet clicked. copies 1 password protected blank sheet (a template) new workbook unprotected sheet. code below used work excel 2007(using .xls format). upgraded excel 2007 excel 2013 , problem appeared.
private sub cmd_click() dim jbook workbook dim jsheet worksheet currentworkbook = activeworkbook.name workbooks(currentworkbook).unprotect jworksheetpassword 'catch errors on error goto errend dim orginalscreenupdating boolean orginalscreenupdating = application.screenupdating application.screenupdating = false if range("language").value = "2" 'french set jsheet = templatefr else 'english set jsheet = templateen end if jsheet.visible = xlsheethidden 'jsheet.visible = xlsheetvisible 'delete line jsheet.unprotect jworksheetpassword set jbook = workbooks.add(xlwbatworksheet) jsheet.copy after:=jbook.sheets(1) jbook.sheets(2).visible = xlsheetvisible application.displayalerts = false jbook.sheets(1).delete application.displayalerts = true jsheet.visible = xlsheetveryhidden 'delete line jbook.sheets(1).unprotect jworksheetpassword 'delete line 'jsheet.protect password:=jworksheetpassword noerrend: workbooks(currentworkbook).protect password:=jworksheetpassword, structure:=true, windows:=false application.screenupdating = orginalscreenupdating unload me exit sub errend: workbooks(currentworkbook).protect password:=jworksheetpassword, structure:=true, windows:=false application.displayalerts = true application.screenupdating = true msgbox datatable.range("msg4").value, vbcritical, datatable.range("msg4title").value unload me end sub
the following lines of code activate original workbook , somehow clears protection of copied sheet excel 2013 only. on excel 2007 causes original workbook activated , confuses users, hence check 2013.
if application.version = "15.0" workbooks(currentworkbook).activate 'jbook.activate end if this hack happens work. if 1 finds better solution please post here well.
the full code listing follows:
private sub cmd_click() dim jbook workbook dim jsheet worksheet currentworkbook = activeworkbook.name workbooks(currentworkbook).unprotect jworksheetpassword 'catch errors on error goto errend dim orginalscreenupdating boolean orginalscreenupdating = application.screenupdating application.screenupdating = false if range("language").value = "2" 'french set jsheet = templatefr else 'english set jsheet = templateen end if jsheet.visible = xlsheethidden set jbook = workbooks.add(xlwbatworksheet) jsheet.copy after:=jbook.sheets(1) jbook.sheets(2).visible = xlsheetvisible application.displayalerts = false jbook.sheets(1).delete application.displayalerts = true if application.version = "15.0" workbooks(currentworkbook).activate 'jbook.activate end if jsheet.visible = xlsheetveryhidden noerrend: workbooks(currentworkbook).protect password:=jworksheetpassword, structure:=true, windows:=false application.screenupdating = orginalscreenupdating unload me exit sub errend: workbooks(currentworkbook).protect password:=jworksheetpassword, structure:=true, windows:=false application.displayalerts = true application.screenupdating = true msgbox datatable.range("msg4").value, vbcritical, datatable.range("msg4title").value unload me end sub
Comments
Post a Comment