N00b problem with #NAME?

tbnz

New Member
Joined
Feb 27, 2022
Messages
4
Office Version
  1. 365
Platform
  1. Windows
For context, I am an experienced now retired programmer with expertise in Access VBA, and a some experience of Excel, but not Excel VBA.
I am trying to create a custom function. As a test, I created this code:
VBA Code:
Function BIRTHDAY()
Dim answer As String
Dim Startdate, Enddate As Date

Startdate = "3/3/1970"
Enddate = "5/6/1970"
answer = Application.WorksheetFunction.Days360(Startdate, Enddate)
Debug.Print answer
BIRTHDAY = answer
End Function
If I run this code it correctly prints 92 in the immediate window.
If I put BIRTHDAY() in a cell, I get #NAME?

I suspect I'm missing something obvious, so please be kind :)
What am I missing?
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try changing the startdate and enddate to a cell reference...then fill those cells with the dates
VBA Code:
Startdate = Cells(1, 1)
Enddate = Cells(2, 1)
 
Upvote 0
Do you have multiple workbooks open? The birthday function needs to be in a module in the same workbook from whence it is being called, otherwise you need to specify where to find the birthday function. Also, Startdate is dimensioned as variant. If you don't specifiy a type then Variant is the default used. Looks like it should be

VBA Code:
Dim Startdate as Date, Enddate As Date
 
Upvote 0
Do you have multiple workbooks open? The birthday function needs to be in a module in the same workbook from whence it is being called, otherwise you need to specify where to find the birthday function. Also, Startdate is dimensioned as variant. If you don't specifiy a type then Variant is the default used. Looks like it should be

VBA Code:
Dim Startdate as Date, Enddate As Date
The module is in "Thisworkbook". Thanks for the heads up about the declaration - I'm pretty rusty these days. In the "real" function the dates will be the date of birth from a cell reference and TODAY().
 
Upvote 0
Put your code in a standard module, not the ThisWorkbook module. Use Insert Module to create a standard module.
 
Upvote 0
Try changing the startdate and enddate to a cell reference...then fill those cells with the dates
VBA Code:
Startdate = Cells(1, 1)
Enddate = Cells(2, 1)
In the "real" function the dates will be the date of birth from a cell reference and TODAY(). So the function invocation in the spreadsheet will be =BIRTHDAY(D2) (for instance) and the function declaration will be function(DOB), so start date and end date will be redundant.
 
Upvote 0
OK, I solved it using the function wizard.
  1. Selected the cell where I wanted the function.
  2. On the "formulas" tab, clicked "insert function".
  3. Selected the category "user defined"
  4. Selected "PERSONAL.XLSB!BIRTHDAY()"
and that was inserted as the formula and returned the correct result,
Nothing I had read anywhere suggested I needed to code the formula that way.

Thanks for the help and suggestions.
 
Upvote 0
Upvote 0
Nothing I had read anywhere suggested I needed to code the formula that way
You only have to do that if the function is in a different workbook that is not an add-in. Also bear in mind that if you need others to use the file, the function won't work for them.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,425
Members
448,961
Latest member
nzskater

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top