excel vba - VBA: Checking a number in an array and proceeding when not present -


i new vba. in below code, attempting search list of numbers in array in dynamic column. if number found, new line inserted. however, error when number in array not found. if there more efficient way same thing, happy change code. appreciated.

sub insertlines()  dim vbusort variant dim vsheet worksheet dim integer vbusort = array("3", "4", "5", "6", "7", "8", "9", "10")      each vsheet in thisworkbook.worksheets         range("a1").select         application.activesheet.range("1:2").find(what:="bu sort priority", _                     lookin:=xlvalues, _                     lookat:=xlwhole, _                     searchorder:=xlbyrows, _                     searchdirection:=xlnext, _                     matchcase:=false).select          activecell.entirecolumn.select          = lbound(vbusort) ubound(vbusort)              selection.find(what:=vbusort(i), after:=activecell, _                 lookat:=xlwhole, searchorder:=xlbycolumns, searchdirection:=xlnext, _                 matchcase:=false).activate                 activecell.entirerow.insert                 activecell.entirerow.interior.color = 6697728          next         end     next vsheet  end sub 

you need capture result of .find() test see if found anything:

sub insertlines()  dim vbusort variant dim vsheet worksheet dim integer dim rng1 range dim rng2 range   vbusort = array("3", "4", "5", "6", "7", "8", "9", "10")   each vsheet in thisworkbook.worksheets         range("a1").select         set rng1 = application.activesheet.range("1:2").find(what:="bu sort priority", _                     lookin:=xlvalues, _                     lookat:=xlwhole, _                     searchorder:=xlbyrows, _                     searchdirection:=xlnext, _                     matchcase:=false).select         if not rng1 = nothing            activecell.entirecolumn.select             = lbound(vbusort) ubound(vbusort)               set rng2 = selection.find(what:=vbusort(i), after:=activecell, _                 lookat:=xlwhole, searchorder:=xlbycolumns, searchdirection:=xlnext, _                 matchcase:=false)                 if not rng2 = nothing                   rng2.select                     activecell.entirerow.insert                     activecell.entirerow.interior.color = 6697728                 end if             next           end         end if   next vsheet  end sub 

also, how avoid using select in excel vba macros


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -