Excel VBA update fill of cells based off of criteria -
my code in vba , updates value of cell based off of 2 criteria. i've commented code extensively, i'll paste below first. i've changed comments adding 2 more apostrophes make bit easier tell difference between comments , code on platform.
sub highlightvalues() '''shortcut key: ctrl + w '''highlights values of corresponding left-most cell, if 2 conditions met: '''the part in "l" class , of rightmost cells empty dim ws worksheet dim long, lastrow long, lastcolumn long, c long, d long, j long, count long, k long, report long set ws = sheets("qap") lastrow = ws.cells(rows.count, 1).end(xlup).row lastcolumn = ws.cells(1, ws.columns.count).end(xltoleft).column '''searches column titles "analysis task count" '''where start searching filled boxes on each row = 1 lastrow if instr(ws.cells(1, i), "analysis task count") '''c number of column analysis task count in c = - 1 end if next '''searches column titles "required rpc" '''where search whether part "l' or not d = 1 lastcolumn if instr(ws.cells(1, d), "required rpc") '''k number of column required rpc in k = d end if next d '''for each row part data in = 11 lastrow count = 0 '''if cells past analysis task count filled, remember j = c lastcolumn if not isempty(ws.cells(i, j)) count = 1 end if next j '''if stage l , cells empty row if cells(i, k).value = ("l") , count = 0 '''highlight first box in green cells(i, 1).interior.color = rgb(102, 255, 102) else '''or if thats not true, make box clear cells(i, 1).interior.color = xlnone report = report + 1 end if next msgbox (report) end sub
to walk through code, (barring spacing, formatting didn't carry over), code first searches column number of 2 critical columns, in last loop, each row, searches whether 2 criteria met. if criteria met, cell turns green, if not, it's filled in blank. i've spent hour confirming values returned k , c both correct column number: don't think issue. also, when set count see how many times code goes through last loop, , each if statement, correct number of cycles.
this frustrating, weird thing code executes instantly, there's no delay @ all. , of course, cell fill doesn't change. test this, ran code filling in cells orange color, , there not change cells.
thank can get!
the part responsible searching analysis task count worksheet doesn't correct me.
for = 1 lastrow '<---- why [lastrow]? should [lastcolumn] if instr(ws.cells(1, i), "analysis task count") '''c number of column analysis task count in c = - 1 '<---- why subtracting 1? end if next
if searching through column titles why use [lastrow] counter upper bound loop. also, don't understand why subtract 1 after find column such header.
because of have incorrect column index , compare incorrect sets of data.
Comments
Post a Comment