How to pass a date as an argument in an Excel VBA function -


i'm trying make function take date in cell argument, use date lookup value. date passed in variable effdate. function should go worksheet ratechgs, check newpymteffdaterange effdate, and, upon finding it, go escrowpymtamtrange (one column wide) , return value on same row there.

i've gotten point of testing in immediate window typing getescrowpymt(8/1/2000) (or other date). value of position variable, can tell function isn't finding date though it's there. problem how i'm passing date?

function getescrowpymt(effdate date) dim pymteffdaterange range  dim escrowpymtamtrange range  dim position integer   set pymteffdaterange = worksheets("ratechgs").range("newpymteffdate") set escrowpymtamtrange = worksheets("ratechgs").range("escrowpymt")  position = application.worksheetfunction.match(effdate, pymteffdaterange, 1) msgbox (position)  end function 

the last argument in match function allows returning approximate match. if require exact match, should use last argument of 0 require exact match. otherwise, using arguments 1 or -1 return approximate match , assume data sorted ascending.

position = application.worksheetfunction.match(effdate, pymteffdaterange, 0) 

the match function error if effdate value not found in lookup array, may need error handling logic account possibility. use application.match function can accept error type, match function in worksheet class accept long/integer values , raise error if value isn't found:

dim position variant position = application.match(effdate, pymteffdaterange, 0) if iserror(position)     msgbox effdate & " not found!", vbinformation      exit function     '   -- or --     '  assign other return value function, etc. end if 

some functions have difficulty working date values, let me know if doesn't solve issue.

vba doesn't play various system locales, if you're expecting "8/1/2000" other august 1, 2000, may have more problems since vba interpret date format, not system locale (e.g., in uk date 8 january, 2000). in case, may best treat date text , match based on text rather date.


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -