What seems to be working now? By modifying your code as Les suggested or by using the UDF that Chris suggested?
 
Perhaps a repost of the corrected code and using CODE TAGS to preserve formatting would be good for others to see your approach as well.