There is nothing wrong with the above function.
If you are calling the above function in VBA then it works as expected and If you are using it as a UDF i.e you are calling it from the worksheet then #Name
error means that you have not pasted the function in a module. Move the function from the Sheet/Workbook code area into a module and then try again.