简体   繁体   English

dd/mm/yyyy 格式的日期在 Excel Vba 中返回 1004 错误

[英]Dates in dd/mm/yyyy format return 1004 Error in Excel Vba

I have this code, and it works:我有这个代码,它有效:

Sub try3()
Dim dates(1 To 2) As Date
Dim values(1 To 2) As Double
Dim TIR As Double

dates(1) = #1/1/2015#
dates(2) = #1/1/2016#

values(1) = -1000
values(2) = 1101

TIR = Application.WorksheetFunction.xirr(values, dates)

End Sub

However, if I change the dates, for example, to但是,如果我更改日期,例如,

dates(1) = #1/15/2015#
dates(2) = #1/15/2016#

, then I get an error 1004: ,然后我收到错误 1004:

"Property Xirr of class WorksheetFuntion could not be obtained". “无法获得类 WorksheetFuntion 的属性 Xirr”。

My computer date format is european (dd/mm/yyyy).我的计算机日期格式是欧洲 (dd/mm/yyyy)。 If I change it to american (mm/dd/yyyy), then my code works.如果我将其更改为美国 (mm/dd/yyyy),那么我的代码就可以工作了。

I would like to keep the european format in my system, and I would like my code to be runnable from any computer, regardless of their system's date format我想在我的系统中保留欧洲格式,并且我希望我的代码可以在任何计算机上运行,​​而不管他们系统的日期格式

I have tried to define the dates as follows, but I get the same error.我试图按如下方式定义日期,但我遇到了同样的错误。

dates(1) = DateSerial(2015, 1, 15)
dates(2) = DateSerial(2016, 1, 15)

Any idea how to make VBA understand dates while keeping the european format in my computer?知道如何让 VBA 理解日期,同时在我的计算机中保留欧洲格式吗?

Thank you谢谢

The problem is not in the XIRR() , it is doing its best:问题不在于XIRR() ,它正在尽其所能:

Sub TestMe()

    Dim TIR As Double
    TIR = Application.WorksheetFunction.Xirr(Array(-1000, 1101), Array(42019, 42384))
    Debug.Print TIR

End Sub

Just try to cast to Long with CLng , whichever date you are working with, it should return 42019 and 42384 in Excel, if you are not using the Date1904 "feature".只需尝试使用CLng转换为Long ,无论您使用哪个日期,它都应该在 Excel 中返​​回4201942384 ,如果您没有使用Date1904 “功能”。

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

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