Keep a fixed ratio between 2 cells Excel vba -
currently im making model in need 2 cells keep fixed ratio such when sum off 2 cells equals one. in case cells f13 , f14. give example if value off f13 gets changed 0.4 f14 should become 0.6. same goes if value off f14 gets changed 0.2 f13 should become 0.8. code got is:
private sub worksheet_change(byval target range) if not intersect(target, target.worksheet.range("f13")) nothing if activecell.value > 1 msgbox "input value larger 1" else range("f14").value = 1 - activecell.value exit sub end if exit sub end if exit sub if not intersect(target, target.worksheet.range("f14")) nothing if activecell.value > 1 msgbox "input value larger 1" else range("f13").value = 1 - activecell.value exit sub end if exit sub end if exit sub end sub
however code not working properly. question therefore how should change code in order working properly?
maybe work you:
private sub worksheet_change(byval target range) dim v variant if target.address = "$f$13" v = target.value if not isnumeric(v) range("f13").value = cverr(xlerrvalue) elseif v < 0 or v > 1 range("f13").value = cverr(xlerrvalue) else range("f14").value = 1 - v end if elseif target.address = "$f$14" v = target.value if not isnumeric(v) range("f14").value = cverr(xlerrvalue) elseif v < 0 or v > 1 range("f14").value = cverr(xlerrvalue) else range("f13").value = 1 - v end if end if end sub
if f13 changed value between 0 , 1 f14 automatically changed complementary value, , vice-versa. if value other number between 0 , 1 put in 1 of 2 cells value in cell becomes error. event handler works if value put directly in either of cells (as opposed larger range containing cells) use of activecell
seems intention.
Comments
Post a Comment