excel - Copying data to a new sheet only if data does not exist using a macro -
i have created simple macro filter data , copy , add specific sheet [isrisks] after last row. realize not sure how check see if data exists in [isrisks] sheet! please - column in [isrisks] contains unique riskid field check. have far:
sub isriskcopy() ' dim lmaxrows long dim ws worksheet dim rng range dim lr long dim rtable range each ws in activeworkbook.worksheets if (ws.name <> "isrisks") , (ws.name <> "closed risks") , (ws.name <> "risk grading matrix ") , (ws.name <> "sheet1") , (ws.name <> "sheet2") application.displayalerts = false ws if .filtermode .autofiltermode = false sheets(ws.name).range("$a$2:$w$2").autofilter field:=5, criteria1:=array("is", "is - information security"), operator:=xlfiltervalues set rtable = sheets(ws.name).autofilter.range.offset(1).specialcells(xlcelltypevisible) rtable.copy lmaxrows = sheets("isrisks").cells(rows.count, "a").end(xlup).row range("a" & lmaxrows + 1).pastespecial paste:=xlpastevaluesandnumberformats, operation:=xlnone, skipblanks:=false, transpose:=false end else end if next ws 'sorting unique id column sheets("isrisks").range("a1").select activeworkbook.worksheets("isrisks").sort.sortfields.clear activeworkbook.worksheets("isrisks").sort.sortfields.add key:=range("a1"), _ sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal activeworkbook.worksheets("isrisks").sort .setrange range("a2:a10000") .header = xlno .matchcase = false .orientation = xltoptobottom .sortmethod = xlpinyin .apply end application.displayalerts = true end sub
you can use code search range , determine if value there.
with sheets("sheet1").range("a:a") set rng = .find(what:=findstring, _ after:=.cells(.cells.count), _ lookin:=xlvalues, _ lookat:=xlwhole, _ searchorder:=xlbyrows, _ searchdirection:=xlnext, _ matchcase:=false) if not rng nothing 'do if found else 'do if not found end if end
you'll need set sheet want use, range want search , replace findstring
variable checking for. sections in if statement can perform actions based on finding or not finding (paste if not found). can redone shorten if don't need perform action if found.
the above example found here.
Comments
Post a Comment