vba - Visual Basic Excel Color Cells on Lost Focus -
vba - Visual Basic Excel Color Cells on Lost Focus -
i need create vba script in excel colors 2 cells when value of 1 @ to the lowest degree 10% greater or less other
private sub worksheet_change(byval target range) application.enableevents = false if target.address = aprx_lns if aprx_lns > aprx2_lns * 0.1 aprx_lns.interior.color = hex(ffff00) aprx2_lns.interior.color = hex(ffff00) elseif aprx_lns < aprx2_lns * 0.1 aprx_lns.interior.color = hex(ffff00) aprx2_lns.interior.color = hex(ffff00) end if end if application.enableevents = true end sub private sub worksheet_change2(byval target range) application.enableevents = false if target.address = aprx2_lns if aprx_lns > aprx2_lns * 0.1 aprx_lns.interior.color = hex(ffff00) aprx2_lns.interior.color = hex(ffff00) elseif aprx_lns < aprx2_lns * 0.1 aprx_lns.interior.color = hex(ffff00) aprx2_lns.interior.color = hex(ffff00) end if end if application.enableevents = true end sub
what doing wrong? neither of cells changing color selected color, after made values create if statement true. know nil vba, explanations great. thanks!
following on comments above, let's combine logic in single event handler.
also, used named ranges/cells, need refer them correctly. name meaningless in vba unless qualified explicit range. pass name string range("aprx_lns")
, etc.
note code fire when alter values of 1 of these 2 cells, directly. means if these cells contain formula referencing other cells, , other cells change, highlighting not occur.
revised & simplified
private sub worksheet_change(byval target range) dim aprx_lns range dim aprx_lns2 range dim difference double dim diffratio double set aprx_lns = range("aprx_lns") '## modify needed set aprx2_lns = range("aprx2_lns") '## modify needed application.enableevents = false if target.address = aprx_lns.address or target.address = aprx2_lns.address difference = abs(aprx_lns) / abs(aprx2_lns) '## compute absolute difference ratio diffratio = abs(1 - difference) if diffratio >= 0.1 '### if cell values differ +/- 10%, highlight them aprx_lns.interior.color = 65535 'vbyellow aprx2_lns.interior.color = 65535 'vbyellow else '### otherwise, unhighlight them: aprx_lns.interior.color = xlnone aprx2_lns.interior.color = xlnone end if end if application.enableevents = true end sub
excel vba excel-vba colors
Comments
Post a Comment