Tuesday, 15 March 2011

vba - Macro Excel Loop -



vba - Macro Excel Loop -

i have problem looping macro in excel. have info base of operations need add together row above each unique value , re-create value below new row. untill have come this:

sub test() ' ' sneltoets: ctrl+k ' findnextvaluechangeincolumn macro ' dim currentvalue string dim comparevalue string currentvalue = activecell.value if (currentvalue = "") selection.end(xldown).select else activecell.offset(1, 0).select comparevalue = activecell.value while currentvalue = comparevalue activecell.offset(1, 0).select comparevalue = activecell.value loop selection.entirerow.insert , copyorigin:=xlformatfromleftorabove activecell.offset(1, 0).select selection.copy activecell.offset(-1, 0).select selection.pastespecial end if exit sub end sub

this macro job, dont want press ctrl-k 4000 times every time update necessary. knows how loop macro ?

just wrap loop around code want executed:

sub test() ' ' sneltoets: ctrl+k ' findnextvaluechangeincolumn macro ' '-------loop 1 4000------------ dim loopy loopy = 1 4000 'loop 4000 times '-------------------------------------- dim currentvalue string dim comparevalue string currentvalue = activecell.value if (currentvalue = "") selection.end(xldown).select else activecell.offset(1, 0).select comparevalue = activecell.value while currentvalue = comparevalue activecell.offset(1, 0).select comparevalue = activecell.value loop selection.entirerow.insert , copyorigin:=xlformatfromleftorabove activecell.offset(1, 0).select selection.copy activecell.offset(-1, 0).select selection.pastespecial end if '-----don't forget line----- next loopy '-------------------------------- exit sub end sub

alternatively, can utilize while loop loop until currentvalue = "":

do currentvalue = activecell.value if (currentvalue = "") selection.end(xldown).select else activecell.offset(1, 0).select comparevalue = activecell.value while currentvalue = comparevalue activecell.offset(1, 0).select comparevalue = activecell.value loop selection.entirerow.insert , copyorigin:=xlformatfromleftorabove activecell.offset(1, 0).select selection.copy activecell.offset(-1, 0).select selection.pastespecial end if loop while currentvalue <> ""

excel vba loops excel-vba

No comments:

Post a Comment