excel - SUMIF and multiple sheets -
i searching multiple sheets string, once string found copies mergeddata sheet.
in column 1 payment number, instance 50
. column 2 work order number 11111
. columns "3,4,5,6,7,8" or other information. "9,10,12" £ values.
when data copied on there multiple instances on work order number in column 2. need have sumif find duplicates in column 2 , sum columns "9, 10 & 12".
i have managed code sumif working can't seem work after data has been copied over.
what need happen is, when click button searches string, , have copy mergeddata sheet, , on same button click perform sumif function, after data has been copied.
any assistance can provide appreciated!
this code using copy data multiple sheets one:
private sub commandbutton1_click() dim firstaddress string, whatfor string dim cell range, sheet worksheet dim ssheetswithdata string, ssheetswithoutdata string dim lsheetrowscopied long, lallrowscopied long dim bfound boolean dim soutput string application .screenupdating = false .enableevents = false .cutcopymode = false end whatfor = sheets("sub con payment form").range("l9") worksheets("mergeddata").cells.clearcontents if whatfor = empty exit sub each sheet in sheets if sheet.name <> "sub con payment form" , sheet.name <> "mergeddata" , sheet.name <> "details" bfound = false sheet.columns(1) set cell = .find(whatfor, lookin:=xlvalues, lookat:=xlwhole) if not cell nothing bfound = true lsheetrowscopied = 0 firstaddress = cell.address lsheetrowscopied = lsheetrowscopied + 1 cell.entirerow.copy activeworkbook.sheets("mergeddata").range("a" & rows.count).end(xlup).offset(1, 0).pastespecial xlvalues set cell = .findnext(cell) loop until cell nothing or cell.address = firstaddress else bfound = false end if if bfound ssheetswithdata = ssheetswithdata & " " & sheet.name & " (" & lsheetrowscopied & ")" & vblf lallrowscopied = lallrowscopied + lsheetrowscopied else ssheetswithoutdata = ssheetswithoutdata & " " & sheet.name & vblf end if end end if next sheet if ssheetswithdata <> vbnullstring soutput = "sheets data copied (# of rows)" & vblf & vblf & ssheetswithdata & vblf & _ "total rows copied = " & lallrowscopied & vblf & vblf else soutput = "no sheets contained data copied" & vblf & vblf end if if ssheetswithoutdata <> vbnullstring soutput = soutput & "sheets no rows copied:" & vblf & vblf & ssheetswithoutdata else soutput = soutput & "all sheets had data copied." end if if soutput <> vbnullstring msgbox soutput, , "copy report" worksheets("mergeddata") if .cells(1, 1).value = vbnullstring .rows(1).delete end set cell = nothing end sub
and other bit of code using sumif:
sub combineduplicates() '### starts our macroapplication.screenupdating = false '### excel wont update screen while executing macro. huge performace boost dim sumcols() '### declare second empty array our sum columns sumcols() = array(9, 10, 12) '### second array stores columns should summed '### next line sets our range searching dublicates. starting @ cell a2 , ending @ last used cell in column set searchrange = range([b2], columns(2).find(what:="*", after:=[b1], searchdirection:=xlprevious)) each cell in searchrange '### start looping through each cell of our searchrange set search = searchrange.find(cell, after:=cell, lookat:=xlwhole) '### searches dublicate. if no dub exists, finds while search.address <> cell.address '### until find our starting cell again, these rows dublicates = 0 ubound(sumcols) '### loop through columns calculating sum '### next line sums cell in our starting row , counterpart in dublicate row cells(cell.row, sumcols(i)) = cdbl(cells(cell.row, sumcols(i))) + cdbl(cells(search.row, sumcols(i))) next '### go ahead next column search.entirerow.delete '### finished row. delete whole row set search = searchrange.find(cell, after:=cell) '### , search next dublicate after our starting row loop next '### here start on next cell of our searchrange '### note: new unique value since deleted old dublicates application.screenupdating = true '### re-enable our screen updating end sub '### ends our macro
Comments
Post a Comment