Excel VBA object variable type mismatch -


i trying determine column number of header in excel spreadsheet using following vba code:

dim acell range set acell = sheet1.rows(1).find(what:="contractdescription", lookin:=xlvalues, _  lookat:=xlwhole, searchorder:=xlbyrows, searchdirection:=xlnext, _  matchcase:=false, searchformat:=false) 

then following line of code gives error:

msgbox (acell.column) 

it gives run-time error 91: "object variable or block variable not set".

according posts have read on internet, error occurs when trying assign value of 1 type variable has been declared different type.

however, trying understand why getting error here since acell range object , taking acell.column, when range("a1").column valid 1 printed on screen.

so may causing problem here?

the reason why error because acell null (no match found). before using range find method (or equivalent, range might nothing), have set condition on lines of:

if (not acell nothing)   msgbox acell.column end if 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -