excel - How do I Copy/Paste specific cells from other worksheet to current worksheet? -
i'm using code take username search of associate info multiple transactions. should paste them current worksheet. seems run, in doesn't throw errors , executes final "select" command, doesn't return pasted data.
option explicit sub investorreport() dim investorname string dim finalrow integer dim integer 'row counter sheets("sheet1").range("d6:k50").clearcontents investorname = sheets("sheet1").range("b3").value finalrow = sheets("investments").range("i1000").end(xlup).row = 2 finalrow if sheets("investments").cells(i, 1) = investorname msgbox ("works") range(cells(i, 2), cells(i, 12)).copy sheets("sheet1").range("d100").end(xlup).offset(1, 0).pastespecial end if next range("b3").select end sub
this code reference sheets:
option explicit sub investorreport() dim investorname string dim finalrow long dim long 'row counter sheets("sheet001") '<--| refer "sheet1" sheet .range("d6:k50").clearcontents '<--| every initial "dot" keep referencing object after "with" keyword ("sheet1" sheet, in case) investorname = .range("b3").value end sheets("investments") '<--| refer "investments" sheet finalrow = .cells(.rows.count, "i").end(xlup).row '<--| .cells , .rows refer "investments" sheet = 2 finalrow if .cells(i, 1) = investorname .range(.cells(i, 2), .cells(i, 12)).copy '<--| .range , .cells refer "investments" sheet sheets("sheet001").range("d100").end(xlup).offset(1, 0).pastespecial '<--| here references explicitly made "sheet1" sheet end if next end sheets("sheet001").range("b3").select end sub
and here follows code avoid loops , exploiting autofilter:
sub investorreport2() dim investorname string dim finalrow long dim long 'row counter sheets("sheet001") '<--| refer "sheet1" sheet .range("d6:k50").clearcontents '<--| every initial "dot" keep referencing object after "with" keyword ("sheet1" sheet, in case) investorname = .range("b3").value end sheets("investments") '<--| refer "investments" sheet .range("a1", .cells(.rows.count, "l").end(xlup)) '<--| refer "investments" sheet columns fom row 1 (header) down last non empty 1 in column .autofilter field:=1, criteria1:=investorname '<--| filter data on first column values matching "investorname" if application.worksheetfunction.subtotal(103, .cells.resize(, 1)) > 1 '<--| if cell has been filtered... .offset(1, 1).resize(.rows.count - 1, .columns.count - 1).specialcells(xlcelltypevisible).copy '<--| ... copy filtered cells skipping headers , first column... sheets("sheet001").range("d100").end(xlup).offset(1, 0).pastespecial '<--| ...and paste them end if end .autofiltermode = false '<--| .. show rows back... end sheets("sheet001").range("b3").select end sub
Comments
Post a Comment