vba - Unable to get the vlookup to work -
i run set of codes , returns error:
run-time error '1004' unable vlookup property of worksheetfunction class.
i have 1 vlookup in sub routine. there wrong code ? debug , error appears @ policybox there.
sub linkpolicynum() dim r integer dim policynum variant dim lookup_num range dim policybox variant r = activecell.row 'row number of selected cell policynum = activesheet.cells(r, 3).value set lookup_num = thisworkbook.sheets("policydetails").range("a1:z5000") policybox = application.worksheetfunction.vlookup(policynum, lookup_num, 3, false) 'to match policy number policy details msgbox policynum msgbox policybox end sub
there appears nothing wrong code. seeing result of happens when use worksheetfunction
version of functions , no result returned. specifically, throw error , interrupt execution of vba. in case, if tried same formula in workbook instead of in vba, form of error (#n/a
or #value!
possibly).
if want prevent happening, easiest thing change using application.vlookup
instead of application.worksheetfunction.vlookup
. although there no intellisense function behaves same other except error handling. if non-worksheetfunction
version of function has error, return error instead of throwing it. allows check error , carry on code.
if think should finding value vlookup
here can start checking mismatches between text/numbers , other things that. check formulas , not in vba though.
here example of using other functional form , trapping error.
sub linkpolicynum() dim r integer dim policynum variant dim lookup_num range dim policybox variant r = activecell.row 'row number of selected cell policynum = activesheet.cells(r, 3).value set lookup_num = thisworkbook.sheets("policydetails").range("a1:z5000") policybox = application.vlookup(policynum, lookup_num, 3, false) 'to match policy number policy details if iserror(policybox) 'possibly "not found" case else msgbox policynum msgbox policybox end if end sub
reference on issue: http://dailydoseofexcel.com/archives/2004/09/24/the-worksheetfunction-method/
Comments
Post a Comment