Break from for loop back into if statement vba -
i trying perform action see if date in range (daterng) less today's date, , then, if is, perform loop hide rows w.here value in column zero. (i paying off loans, , every month want hide loans have been paid off.) months across columns, loans in rows. loan balance (i, j).
the problem never exits loop go , check date after every new 'j' (column). stays in loop. have tried break, exit, continue, etc. none seem work, @ least place them. how check date, compare 'today', run loop check each cell in column, before moving on column 2, checking date , performing same loop.
it have dynamic, not necessary, every month change ranges in code. strictly personal use. appreciated. thank you.
sub hidepaid() dim day range, loanrng range, loansum worksheet, daterng range, cel2 range, long, j long, col range set loansum = thisworkbook.worksheets("loan sum") loansum.activate set daterng = activesheet.range("d2:r2") set loanrng = activesheet.range("d4:r16") each day in daterng if day.value < date j = 1 loanrng.columns.count = 1 loanrng.rows.count if loanrng.cells(i, j).value < 1 loanrng.cells(i, j).entirerow.hidden = true end if next next j end if next end sub
i added comments in code show changes.
you close, had 1 many loops , say, needed find right place exit.
sub hidepaid() dim day range dim loanrng range dim loansum worksheet dim daterng range dim long set loansum = thisworkbook.worksheets("loan sum") loansum.activate set daterng = activesheet.range("d2:r2") set loanrng = activesheet.range("d4:r16") 'this loop processes column each day in daterng 'once date in column greater today, stop processing 'it assumes values in daterng valid dates '(i.e. '01/01/2016' not 'jan', can use number format in excel 'get date show 'jan' if better you) if datediff("d", now(), day.value) > 0 exit 'the line of code had should have worked in sense, 'it have touched every column procesed before today 'it assumes value in cell actual date 'if day.value < date 'you not need column loop here in 1 in 'previous loop 'for j = 1 loanrng.columns.count 'this loop processes rows not hidden , if 'the value equal 0 hides row 'note: had check less 1, .50 less 1 , don't 'want caught out on loan! = 1 loanrng.rows.count if (loanrng.cells(i, day.column - 3).value = 0) , (loanrng.cells(i, day.column - 3).entirerow.hidden = false) loanrng.cells(i, day.column - 3).entirerow.hidden = true end if next next 'its practice clear out resources when finishing set daterng = nothing set loanrng = nothing set loansum = nothing end sub
Comments
Post a Comment