Using an if statement within a for loop- Excel VBA -
i'm having trouble using if statement inside loop in excel vba. output of debugger not expect. can post full code of trying accomplish, think have narrowed down don't understand. here code:
private sub worksheet_change(byval target range) dim integer = 9 60 step 3 if cells(i, "dr").value < cells(i, "eb").value debug.print & "-ifloopstart" cells(i, "dr").value = 999999 debug.print & "-ifloopend" end if next end sub
the output of debugger is:
9-ifloopstart 33-ifloopstart 51-ifloopstart 51-ifloopend 33-ifloopend 9-ifloopend
however, expected:
9-ifloopstart 9-ifloopend 33-ifloopstart 33-ifloopend 51-ifloopstart 51-ifloopend
can explain how works? seems looping beginning of if statement instead of finishing if statement. how can modify code output expect? i've been struggling hours , seems simple :( .
each time worksheet updated cells(i, "dr").value = 999999
, worksheet_change
gets called again.
think of way. each time above code gets called, modify cell, triggers worksheet change method again.
so nesting 3 calls of function:
- called once, = 9
- called again, = 33
- called again, = 51
- finishes = 51 call
- finishes = 33 call
- called again, = 33
- finishes = 9 call
vba goes backwards each of these first time method run.
edit: tim says can disable application.enableevents=false
Comments
Post a Comment