Excel VBA对象必需的错误

我有两行代码,看起来非常容易,但是Excel不断给我这个编译错误告诉我,对象是必需的?

所以基本上我想获得当前的时间,并用下划线replace空格,所以我可以使用这个string作为我的日志文件的名称。

Dim name As String 'EXCEL GIVE ME compile error: object required name = Replace(FormatDateTime(Now, DateFormat.LONGTIME), " ", "_") 

怎么了?!!

replaceDateFormat.LONGTIME为'vbLongTime'适用于我。

 name = Replace(FormatDateTime(Now, vbLongTime), " ", "_") 

你需要调用Now,因为它不是一个variables,而是一个过程

 var now = str(Now()); name = Replace(FormatDateTime(now, DateFormat.LONGTIME), " ", "_") 

应该修复它