Got a loop in Excel sheet vba -
i doing sheet has cell let user put in downpayment amount displays percent is. have row has unlocked cell user enter percent , fill in downpayment amount. know awkward , want have 1 cell downpayment , 15% having trouble writing formula it. because skill bad, used event handler on "sheet1 change" looks change in fields , hides 1 hasn’t been used bigger problem have button on sheet clear of unlocked cells @ once when calculations done. "clear cells" button clears cells have referenced in "worksheet change" code , causes loop.
if there better way enter formula on bar have 1 row has both cell percent , cell downpayment amount , have user enter 1 of them , other autopopulates. ie: downpayment/total amount fills % cell. or if entered percent percent * total amount fills dollar value in downpayment amount. sorry if confusing trying keep clear.
private sub worksheet_change(byval target range) if target.address = "$b$5" msgbox "down payment percent chosen" 'want change range a6 d6 hidden or locked range("6:6").entirerow.hidden = true end if if target.address = "$d$6" msgbox "down payment amount chosen" 'range("b5").value = 0 range("5:5").entirerow.hidden = true end if 'if target.address = "$d$6" ' msgbox "amount chosen" ' rows("5:5").entirerow.hidden = false 'end if 'if target.address = "$b$5" ' msgbox "% chosen" ' rows("6:6").entirerow.hidden = false 'end if end sub sub cleartest() dim ws worksheet dim rrng range dim rcell range dim rrows range set rrng = sheet1.range("a1:d28") 'need bring hidden row if rows("5:5").entirerow.hidden = true rows("5:5").entirerow.hidden = false end if if rows("6:6").entirerow.hidden = true rows("6:6").entirerow.hidden = false end if each rcell in rrng.cells if rcell.locked = false msgbox "testing" debug.print rcell.address, rcell.value rcell.clearcontents end if next rcell 'got internet end sub
sorry, have no idea how make code formatted in grey easy viewing here. see of in grey don't know why or how. selected of code , pressed code button above can see use :)
thank carol
disable events temporarily:
sub cleartest() dim rrng range, rcell range set rrng = sheet1.range("a1:d28") rows("5:5").entirerow.hidden = false '<--| no need check if row hidden, set visible rows("6:6").entirerow.hidden = false '<--| no need check if row hidden, set visible application.enableevents = false '<--| disable events handling on error goto errorhandler '<--| sure catch error , enable events handling each rcell in rrng.cells if rcell.locked = false msgbox "testing" debug.print rcell.address, rcell.value rcell.clearcontents end if next rcell errorhandler: application.enableevents = true'<--| enable events handling end sub
edit: added change event code same technique
private sub worksheet_change(byval target range) application.enableevents = false '<--| disable events handling on error goto errorhandler '<--| sure catch error , enable events handling select case target.address case "$b$2" ' downpayment inout range("b3") = (range("b2").value / range("b1").value) * 100 case "$b$3" ' percent entered range("b2").value = (range("b1").value * range("b3").value) / 100 end select errorhandler: application.enableevents = true'<--| enable events handling end sub
Comments
Post a Comment