VBA Excel inputbox run time error 13 type mismatch -


i have macro working well. issue have inputbox. macro running errors when user presses cancel or x out of inputbox. the user can input 15 values search , enters 0 search begin. want make more robust wont run these errors. please help.

sub findvalues()         dim lsearchrow integer     dim rw integer, cl range, lsearchvalue long, lcopytorow integer     dim ihowmany integer     dim asearch(15) long     dim integer      ' clear sheets before runs accurate number of funds opend.      sheet2.cells.clearcontents     sheets("tier 2").cells.clearcontents     sheets("tier 3").cells.clearcontents     sheets("tier 4").cells.clearcontents     sheets("tier 5").cells.clearcontents      on error goto err_execute     fixc     sheet2.cells.clear     sheet1.select     ihowmany = 0     lsearchvalue = 99      'this end user input required a/c searched      while lsearchvalue <> 0         lsearchvalue = inputbox("please enter value search for. enter 0 indicate finished" & _     "entry.", "enter search value")         if lsearchvalue <> 0             ihowmany = ihowmany + 1             if ihowmany > 15                 msgbox "you limited 15 search numbers.", vbokonly, "limit reached"                 ihowmany = 15                 exit             end if             asearch(ihowmany) = lsearchvalue         end if     loop      if ihowmany = 0         msgbox "no selections entered.", vbokonly + vbcritical, "no search data"         exit sub     end if      lcopytorow = 2      rw = 1 1555         each cl in range("d" & rw & ":m" & rw)         '------------------------------------------------             = 1 ihowmany                 debug.print cl.row & vbtab & cl.column                 lsearchvalue = asearch(i)                 if cl = lsearchvalue                     cl.entirerow.copy                     'destination:=worksheets("sheet2")                     '.rows(lcopytorow & ":" & lcopytorow)                     sheets("sheet2").select                     rows(lcopytorow & ":" & lcopytorow).select                     'selection.pastespecial paste:=xlpastevaluesandnumberformats                     selection.pastespecial paste:=xlpastevaluesandnumberformats, operation:= _                        xlnone, skipblanks:=false, transpose:=false                     'move counter next row                     lcopytorow = lcopytorow + 1                     'go sheet1 continue searching                     sheets("sheet1").select                 end if             next             'lsearchrow = lsearchrow + 1         next cl     next rw     'position on cell a3     'application.cutcopymode = false     'selection.copy     sheets("sheet2").select     cells.select     selection.pastespecial paste:=xlpasteformats, operation:=xlnone, _         skipblanks:=false, transpose:=false      application.cutcopymode = false     sheet2.select     msgbox "all matching data has been copied." exit sub 

define string variable dim lsearchstring string , assign return value of inputbox: lsearchstring = inputbox(...). can check input being numeric: if isnumeric(lsearchstring) , handle wrong input. if successful can proceed convert integer: lsearchvalue = cint(lsearchstring). have know cint() returns 0 if argument not number.

the following code snippet modified extract code. wrong inputs ignored.

update

dim lsearchvalue integer dim lsearchstring string  '..........  lsearchvalue = 99  while true     lsearchstring = inputbox( _            "please enter value search for. " & _            "enter 0 indicate finished entry", _            "enter search value")      if isnumeric(lsearchstring)         lsearchvalue = cint(lsearchstring)         if lsearchvalue = 0 exit          ihowmany = ihowmany + 1         if ihowmany > 15             msgbox "you limited 15 search numbers.", vbokonly, "limit reached"             ihowmany = 15             exit         end if         asearch(ihowmany) = lsearchvalue     end if loop 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -