excel - For or While loop to find data and paste it into correct location -
this simple questions have been unable find answer. i'm trying iterate through column of operator names on 1 worksheet appear on worksheet in same workbook. each name want trigger if statement pulls data original worksheet pastes data in cell next operator's name. problem i'm having don't know how set loops. code below, know mess because cannot loop variables defind correctly (it gives compile error "next without for" right now). appriciated.
sub operatorscrap() dim str_datemin string dim str_datemax string dim datemin date dim datemax date dim lastrow long dim subtotal double dim lookupdate date dim subtotal2 dim oprange dim orange variant dim opname dim scraprange dim scrapr variant oprange = "b32:b" & range("b" & cells.rows.count).end(xlup).row scraprange = "c32:c" & range("c" & cells.rows.count).end(xlup).row lastrow = sheet1.cells(rows.count, 1).end(xlup).offset(0, 0).row subtotal = 0 subtotal2 = 0 str_datemin = inputbox("input beginning date, mm/dd/yyyy:") str_datemax = inputbox("input end date, mm/dd/yyyy:") datemin = cdate(str_datemin) datemax = cdate(str_datemax) scrapr = 1 scraprange orange = 1 oprange while oprange = "daniel" oprange = oprange + 1 lrow = 2 lastrow lookupdate = sheets("sheet1").cells(lrow, "ar").value opname = "daniel" if datemin <= lookupdate , lookupdate <= datemax , sheets("sheet1").cells(lrow, "a").value _= opname subtotal = subtotal + sheets("sheet1").cells(lrow, "av").value subtotal2 = subtotal2 + sheets("sheet1").cells(lrow, "n").value end if next lrow next orange if subtotal2 <> 0 sheets("scrap").activate range("c32").value = (subtotal) / subtotal2 end if if subtotal2 = 0 activesheet.range("b32").entirerow.delete end if next scrapr subtotal = 0 subtotal2 = 0 loop end sub
declare range variables ranges:
dim oprange range dim scraprange range dim long 'use counter in loops
assign them thusly:
set oprange = range("b32:b" & range("b" & cells.rows.count).end(xlup).row)
assign scraprange using offset
method: 1 column right of oprange
:
set scraprange = oprange.offset(0,1)
then:
lastrow = sheet1.cells(rows.count, 1).end(xlup).offset(0, 0).row subtotal = 0 subtotal2 = 0 str_datemin = inputbox("input beginning date, mm/dd/yyyy:") str_datemax = inputbox("input end date, mm/dd/yyyy:") datemin = cdate(str_datemin) datemax = cdate(str_datemax) 'since oprange , scraprange same size, can iterate them in parallel ' counter variable, "i" = 1 oprange.cells.count 'you can remove these 2 lines, use them debug msgbox oprange.cells(i, 1).value msgbox scraprange.cells(i, 1).value 'i think need: ' sets opname based on currenty "i" cell in iteration of oprange ' each iteration of "i" give different value based on oprange opname = oprange.cells(i, 1).value 'this iterates worksheet , lookup: lrow = 2 lastrow lookupdate = sheets("sheet1").cells(lrow, "ar").value if datemin <= lookupdate , _ lookupdate <= datemax , _ sheets("sheet1").cells(lrow, "a").value = opname subtotal = subtotal + sheets("sheet1").cells(lrow, "av").value subtotal2 = subtotal2 + sheets("sheet1").cells(lrow, "n").value end if next lrow if subtotal2 <> 0 sheets("scrap").range("c32").value = (subtotal) / subtotal2 end if if subtotal2 = 0 sheets("scrap").range("b32").entirerow.delete end if 'reset subtotals: subtotal = 0 subtotal2 = 0 next
Comments
Post a Comment