excel - If entered word is found in worksheet, then excute A, if not found, then excute B -


i tried write macro if enter 3-letter month abbreviation , it's found in working sheet, executed; else (if not found), let message box pop ("what entered doesn't match anything").

below code. works when there's match, when there's no match or wrong-typed 3-letter word, there's debug message - " run time error '91': object variable or block variable not set".

the line triggers error is:

 if sheets("sheet1").cells.find(what:=findmonth, _  lookin:=xlformulas, lookat:=xlwhole).column <> "0" 

can tell what's wrong it? how can fix error? thank you.


sub cdaenter() dim rwcrow integer dim mscolumn1 integer dim mscolumn2 integer dim mscolumn3 integer dim mscolumn4 integer dim pdrrow1 integer dim pdrrow2 integer dim pdrrow3 integer dim pdrrow4 integer  dim mcolumn integer dim findmonth string  rwcrow = sheets("sheet2").cells.find(what:="rwc").row  mscolumn1 = sheets("sheet2").cells.find(what:="abc").column  mscolumn2 = sheets("sheet2").cells.find(what:="def").column  mscolumn3 = sheets("sheet2").cells.find(what:="ghi", lookin:=xlvalues, lookat:=xlwhole).column  mscolumn4 = sheets("sheet2").cells.find(what:="jkl").column  pdrrow1 = sheets("sheet1").cells.find(what:="mno").row pdrrow2 = sheets("sheet1").cells.find(what:="pqr").row pdrrow3 = sheets("sheet1").cells.find(what:="stu").row pdrrow4 = sheets("sheet1").cells.find(what:="vwx").row  findmonth = inputbox("enter 3-letter working month ")  if sheets("sheet1").cells.find(what:=findmonth, lookin:=xlformulas, lookat:=xlwhole).column <> "0"  mcolumn = sheets("sheet1").cells.find(what:=findmonth, lookin:=xlformulas, lookat:=xlwhole).column  sheets("sheet2").cells(rwcrow, mscolumn1).copy destination:=sheets("sheet1").cells(pdrrow1, mcolumn)  sheets("sheet2").cells(rwcrow, mscolumn2).copy destination:=sheets("sheet1").cells(pdrrow2, mcolumn)  sheets("sheet2").cells(rwcrow, mscolumn3).copy destination:=sheets("sheet1").cells(pdrrow3, mcolumn)  sheets("sheet2").cells(rwcrow, mscolumn4).copy destination:=sheets("sheet1").cells(pdrrow4, mcolumn)  exit sub  elseif iserror(mcolumn) = true msgbox "what entered doesn't match anything" exit sub end if  end sub 

summing comments, i'll post answer (i know guys maybe busy post answer).
should of them decide post comment answer, i'll delete mine.

first declare variable:

dim r range ' find method returns range object, you'll need variable hold  set r = sheets("sheet1").cells.find(what:=findmonth, _         lookin:=xlformulas, lookat:=xlwhole)          'i copied above if statement since said works 

as tim mentioned, check if found something:

if not r nothing     'you put rest of code here else     msgbox "what entered doesn't match anything.", vbinformation     'inform user through message box end if 

if still have problems, in attempts, please add comment or revise question.
if entirely different issue, post another.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -