vba - Clearing cell contents prevent next button to be added -
i want replace cell values buttons, clearing value prevents next button added. here's script:
private sub workbook_open() dim sh worksheet dim btn button dim cell range each sh in activeworkbook.worksheets each cell in sh.usedrange.cells if cell.value = "{magic_value}" set btn = sh.buttons.add(cell.left, cell.top, cell.width, cell.height) btn .onaction = "buttonclickhandler" .caption = "btn_" & cell.left & "_" & cell.top .name = "btn_" & cell.left & "_" & cell.top end cell.clearcontents ' works without line end if next next end sub
when open workbook, first button added next buttons not, , runtime error '1004'
occurs: unable property add of class buttons
(my translation). , debugger points line set btn = sh.buttons.add ...
from research, reason line cell.clearcontents
. can't imagine how error related cell clearing.
i'm sorry, problem in code didn't show you:
private sub workbook_sheetchange(byval sh object, byval target range) sh.unprotect pwd target.locked = false sh.protect pwd end sub
updating cell's value raised sheetchange event. solution temporarily disable events:
application.enableevents = false each sh in activeworkbook.worksheets ... application.enableevents = true
Comments
Post a Comment