简体   繁体   English

Excel VBA:NetworkDays错误2015

[英]Excel VBA: NetworkDays Error 2015

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. 我在VBA类中有这段代码,它可以锻炼类的属性(日期)和今天的日期之间的天数。

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. 我已经引用了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. 编辑:我发现,当我通过菜单栏上创建的菜单选项运行代码时,它会失败,但是当我通过按钮或通过VB编辑器运行代码时,它可以正常工作。 Looks like it is something to do with the menu. 看起来这与菜单有关。

Thanks, Martin 谢谢马丁

Are you sure you pasted the code correctly into your question? 您确定将代码正确粘贴到您的问题中吗? NetworkDays takes 3 arguments, not one, so: NetworkDays接受3个参数,而不是1个,因此:

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

should be: 应该:

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

When you are running from Menu you are running at application level context. 从Menu运行时,您正在应用程序级别上下文中运行。 So Range("BankHolidays") is not resolved properly if the range is not defined or defined in muliple open workbooks. 因此,如果未定义范围或未在多个打开的工作簿中定义范围,则不能正确解析Range(“ BankHolidays”)。 Try using 尝试使用

activeworkbook.activesheet.Range("BankHolidays")

That should resolve the name. 那应该解决名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM