views:

179

answers:

3

Hi All,

I have the this bit of code in a VBA class which is to workout the number of days between a property of the class (a date) and today’s date.

Dim EmailDate As Date
EmailDate = Me.Email.DateReceived

Debug.Print EmailDate, Date
Debug.Print NetworkDays(EmailDate), Date, Range("BankHolidays"))

When I run it I get the following output

23/04/2010    19/05/2010 
[GetMacroRegId] 'NETWORKDAYS' <
[GetMacroRegId] 'NETWORKDAYS' -> '699990072' >
Error 2015

I have tested it in a module, using dummy data, and get the correct answer. Can anyone see why this would be giving an error in a class?

I have referenced atpvbaen.xls.

Edit: I have found that when I run the code through a menu option I have created on the menu bar it fails, but when I run it via a button or through the VB Editor it works fine. Looks like it is something to do with the menu.

Thanks, Martin

A: 

From what I can tell, it might be a bug to do with Excel 2007 - is that what you are using?

Have a look here for a similar issue.

Dave Arkell
Hi Dave, I have had a look over the link and I don't think that it is related. I am using 2003 and only seem to be getting the error when I run the code from the a menu bar option. Thanks, Martin
Martin
A: 

When you are running from Menu you are running at application level context. So Range("BankHolidays") is not resolved properly if the range is not defined or defined in muliple open workbooks. Try using

activeworkbook.activesheet.Range("BankHolidays")

That should resolve the name.

Adarsha
Hi, I have tried your suggestion but it hasn't made any difference I am still getting the error. I have written a function that produces the same results but doesn't use the function. Thanks for your help.
Martin
A: 

Are you sure you pasted the code correctly into your question? NetworkDays takes 3 arguments, not one, so:

Debug.Print NetworkDays(EmailDate), Date, Range("BankHolidays")) 

should be:

Debug.Print NetworkDays(EmailDate, Date, Range("BankHolidays")) 
PowerUser
Hi, the second ) was a typo when I was putting together the post. Removing it doesn't solve the problem.
Martin
Just trying to rule out the obvious. Good luck.
PowerUser