excel - Copy Rows from Multiple Sheets and sum values -
i have worksheet every month jan dec , worksheet called report copied data goes
in months sheet have following data
id name # days of vacation 1 george 3 2 mary 5
every month sheet has same names names not bound same id want in summary sheet
id name # days of vacation month 1 george 3 jan 2 george 2 feb sum george 5 year
what have managed copy 1 month sheet report can't copy multiple month sheets , don't know how sum part. ideas?
sub searchforstring() dim lsearchrow integer dim lcopytorow integer on error goto err_execute 'start search in row 2 lsearchrow = 2 'start copying data row 2 in sheet2 (row counter variable) lcopytorow = 2 fname = inputbox("enter name", "enter data") if fname = "" while fname = "" msgbox ("enter name") fname = inputbox("enter name", "enter data") wend end if while len(range("a" & cstr(lsearchrow)).value) > 0 'if value in column e = "mail box", copy entire row sheet2 if sheets("jan").range("b" & cstr(lsearchrow)).value = fname 'select row in sheet1 copy rows(cstr(lsearchrow) & ":" & cstr(lsearchrow)).select selection.copy 'paste row sheet2 in next row sheets("report").select rows(cstr(lcopytorow) & ":" & cstr(lcopytorow)).select activesheet.paste 'addwatermark ("jan") 'move counter next row lcopytorow = lcopytorow + 1 end if 'go sheet1 continue searching sheets("report").select lsearchrow = lsearchrow + 1 wend 'position on cell a3 application.cutcopymode = false range("a3").select msgbox "copy done" exit sub err_execute: msgbox "error" end sub
what you'll need loop through each of month sheets, , while on each sheet, loop through data find values on sheet match name indicated name (fname
in code). 2 loops total. also, check out this link starter, should avoid selecting , copying when writing vba code.
the basic code you'd looking (with assumptions made) following
'assuming jan first sheet , dec 12th sheet in workbook lcopytorow = 2 = 1 12 lsearchrow = 2 while len(sheets(i).cells(lsearchrow,1).value) > 0 if sheets(i).cells(lsearchrow,2).value = fname sheets("summary").range(sheets("summary").cells(lcopytorow,1), _ sheets("summary").cells(lcopytorow,3)) = _ range(sheets(i).cells(lsearchrow,1), _ sheets(i).cells(lsearchrow,3)).value lcopytorow = lcopytorow + 1 end if lsearchrow = lsearchrow + 1 loop next
Comments
Post a Comment