excel database function in combination with vba, what if there are no records? -


i'm using database function of excel. see example image

enter image description here

i use vba select records have 'yes' lets a

selection.autofilter field:=2, criteria1:="yes" range("b3").select range(selection, selection.end(xldown)).select 

i copy paste somewhere else. example:

selection.copy range("b12").select activesheet.paste 

the problem when there no records yes, error 1004. because there nothing paste. how write script if there nothing paste, exits sub?

i tried things counta no succes.

your appreciated! :)

i doing way because don't need error check it. if there no results, paste blank cell:

sub tgr()      range("b2").currentregion         .autofilter 2, "yes"         intersect(.offset(1), columns("b")).copy range("b12")         .autofilter     end  end sub 

alternately, if have 1 criteria, use countif test if criteria exists before performing filter:

sub tgr()      dim strcriteria string      strcriteria = "yes"      range("b2").currentregion         if worksheetfunction.countif(intersect(.cells, columns("c")), strcriteria) > 0             .autofilter 2, strcriteria             intersect(.offset(1), columns("b")).copy range("b12")             .autofilter         else             msgbox "no cells found contain """ & strcriteria & """", , "no matches"         end if     end  end sub 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -