excel - Change Background when formula removed by user change -
aim:
change background colour if there no formula (when user overrides defaults formula goes , needs highlighted)
private sub worksheet_change(byval target range) set currentsheet = activeworkbook.sheets("audit findings") '############# 'check if missing formulas not allowed dim rng range dim row range dim cell range set rng = currentsheetrange("j7:j11") each cell in rng if cell.hasformula range(cell.address).interior.colorindex = 37 ' msgbox "cell " & cell.address & " contains formula." else range(cell.address).interior.color = rgb(255, 0, 0) 'msgbox "the cell has no formula." end if next cell '############# 'check if blanks not allowed on error goto whoa application.enableevents = false 'set range check if not intersect(target, range("e7:j11")) nothing 'check length , reverse if blank has value '################# if len(trim(target.value)) = 0 application.undo end if letscontinue: application.enableevents = true exit sub whoa: msgbox err.description resume letscontinue end sub
references:
your code works fine (except in code provided in both cases change interior color red).
i move code worksheet_change
event, , scan cells being modifed fall inside scanned range (the for each cell in rng
loop not needed anymore)
private sub worksheet_change(byval target range) dim watchrange range dim intersectrange range ' modify range suit needs set watchrange = range("j7:j11") set intersectrange = intersect(target, watchrange) if not intersectrange nothing if target.hasformula target.interior.color = rgb(0, 255, 0) ' has formula >> color green ' msgbox "cell " & cell.address & " contains formula." else target.interior.color = rgb(255, 0, 0) ' has no formula >> color red 'msgbox "the cell has no formula." end if end if end sub
Comments
Post a Comment