vba 1004 运行时错误,因为 Excel 找不到文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18417641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:57:21  来源:igfitidea点击:

1004 run-time error as Excel cannot find the file

excelexcel-vbavba

提问by whytheq

I use a virtual pc to run my macros.

我使用虚拟电脑来运行我的宏。

Currently when I remote into this pc I find Excel open with a run-time error 1004 describing File "Daily_Summary.xlsx" cannot be foundand when I open the VBAeditor it has the following line highlighted:

目前,当我远程访问这台电脑时,我发现 Excel 打开时出现运行时错误 1004 描述File "Daily_Summary.xlsx" cannot be found,当我打开VBA编辑器时,它突出显示了以下行:

Excel.ActiveWorkbook.SaveAs "C:\PDFfiles\Daily_Summary.xlsx"

At this point Daily_Summary.xlsxdoes not exist, until it has been saved so I wouldn't expect the program to be able to find the file - therefore the error message seems strange.

此时Daily_Summary.xlsx不存在,直到它被保存,所以我不希望程序能够找到该文件 - 因此错误消息看起来很奇怪。

Initially I thought maybe it would be network issues due to it being a virtual pc but the line of code is trying to save the file locally to the c-driveso how can it be a network issue?

最初我认为这可能是网络问题,因为它是虚拟电脑,但代码行试图将文件本地保存到本地,c-drive所以怎么可能是网络问题?

Anybody ever experienced anything like this before?

有没有人以前经历过这样的事情?

采纳答案by varocarbas

Your code should work fine in case of referring to a file different than the current one (from which the macro is executed). You would get a (1004) error message only if this file is not accessible for some reason.

如果引用的文件不同于当前文件(执行宏的文件),您的代码应该可以正常工作。仅当由于某种原因无法访问此文件时,您才会收到 (1004) 错误消息。

On the other hand, if you are intending to save the current file (from which the macro is being executed), an error would be triggered every time because of intending to save it as a XLSX file. A file containing macros has to be stored as a macro-supporting format (e.g., XLSM). If you try to save it by relying on a wrong file type (like XLSX), you would get a prompt explaining the problem. But, if Application.DisplayAlerts = Falseis present in your code, you would get the standard 1004 error message when the file cannot be accessed (as in the example above).

另一方面,如果您打算保存当前文件(从中执行宏),由于打算将其保存为 XLSX 文件,因此每次都会触发错误。包含宏的文件必须存储为宏支持格式(例如,XLSM)。如果您尝试通过依赖错误的文件类型(如 XLSX)来保存它,您将收到解释问题的提示。但是,如果Application.DisplayAlerts = False您的代码中存在 1004 错误消息,则无法访问该文件时(如上例所示),您将收到标准的 1004 错误消息。