vba - Excel "Do While" Loop Not functioning correctly after first iteration -
i have sub light formatting, , need evaluate , count whether column contains "1" or nothing, , if column has header isnumeric or not.
first iteration of do...until loops functions should. however, if try run second time, throws active cell way rightmost column in worksheet (xfd). have total of 114,000 rows need loop through.
please see code below, first loop; need nested inside loop cycling through rows:
sub totalbookcountsprocess() dim ws excel.worksheet dim numberedbooks integer 'total number of physical books dim virtualbooks integer 'total number of virtual books dim firstbookcol integer 'first column book number dim ispeeccol integer 'ispec column dim lastworksheetcol integer 'last column in worksheet after adding total book count columns dim loopcoloffset integer 'offset column amounts new row reset after loop dim lastitem string 'last item number in last row of worksheet activecell.end(xldown).select lastitem = activecell.value activecell.end(xlup).end(xltoright).select activecell.offset(0, 1).select activecell.value = "total numbered books" activecell.offset(0, 1).select activecell.value = "total cs books" lastworksheetcol = activecell.column columns.autofit numberedbooks = 0 virtualbooks = 0 cells.range("1:1").find("ispec").select ispeccol = activecell.column firstbookcol = ispeccol + 1 activecell.offset(1, 1).select loopcoloffset = ((lastworksheetcol - firstbookcol) * -1) until activecell.end(xlup).value = "total numbered books" if activecell.value = 1 , isnumeric(activecell.end(xlup).value) = true numberedbooks = numberedbooks + 1 activecell.offset(0, 1).select elseif activecell.value = 1 , isnumeric(activecell.end(xlup).value) = false virtualbooks = virtualbooks + 1 activecell.offset(0, 1).select else activecell.offset(0, 1).select end if loop activecell.value = numberedbooks activecell.offset(0, 1).select activecell.value = virtualbooks activecell.offset(1, loopcoloffset).select end sub
any insights appreciated.
the reason lies in
activecell.end(xlup).value = "total numbered books"
as ending condition of loop
your real goal end row loop activecell
column 1 "total numbered books" value in first row
but
activecell.end(xlup).value
refer first non empty cell aboveactivecell
from second iteration on, cell column first row value "total numbered books" has cell right above filled
numberedbooks
valueso keeps skipping next column till end of columns...
your code follows:
option explicit sub totalbookcountsprocess() dim ws excel.worksheet dim numberedbooks integer 'total number of physical books dim virtualbooks integer 'total number of virtual books dim firstbookcol integer 'first column book number dim ispeeccol integer 'ispec column dim lastworksheetcol integer 'last column in worksheet after adding total book count columns dim loopcoloffset integer 'offset column amounts new row reset after loop dim lastitem string 'last item number in last row of worksheet dim ispeccol long activecell.end(xldown).select lastitem = activecell.value activecell.end(xlup).end(xltoright).select activecell.offset(0, 1).select activecell.value = "total numbered books" activecell.offset(0, 1).select activecell.value = "total cs books" lastworksheetcol = activecell.column columns.autofit numberedbooks = 0 virtualbooks = 0 cells.range("1:1").find("ispec").select ispeccol = activecell.column firstbookcol = ispeccol + 1 activecell.offset(1, 1).select loopcoloffset = ((lastworksheetcol - firstbookcol) * -1) numberedbooks = 0 virtualbooks = 0 until cells(1, activecell.column) = "total numbered books" if activecell.value = 1 if isnumeric(cells(1, activecell.column)) numberedbooks = numberedbooks + 1 else virtualbooks = virtualbooks + 1 end if end if activecell.offset(0, 1).select loop activecell.value = numberedbooks activecell.offset(0, 1).select activecell.value = virtualbooks activecell.offset(1, loopcoloffset).select loop until cells(activecell.row - 1, 1) = lastitem end sub
where added rows loop
but sure real solution avoiding selecting/activating
Comments
Post a Comment