1004运行时错误,因为Excel找不到文件 [英] 1004 run-time error as Excel cannot find the file

查看:1207
本文介绍了1004运行时错误,因为Excel找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



目前,当我远程进入这个电脑时,我发现Excel打开了一个运行时错误1004描述文件Daily_Summary.xlsx无法找到,而当我打开 VBA 编辑器时,它突出显示以下行:

  Excel.ActiveWorkbook.SaveAsC:\PDFfiles\Daily_Summary.xlsx

此时, Daily_Summary.xlsx 不存在,直到保存为止,所以我不会指望程序能够找到文件 - 因此错误消息似乎很奇怪。



最初我以为也许是网络问题,因为它是一个虚拟的电脑,但是代码行正在尝试将文件保存在 c-drive 中,所以如何才能成为网络问题?



<有人曾经经历过这样的事情?

解决方案

你的代码应该适用于引用ng到不同于当前宏的文件(执行宏)。只有当这个文件由于某种原因无法访问时,您才会得到(1004)错误消息。



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

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 found and when I open the VBA editor it has the following line highlighted:

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

At this point Daily_Summary.xlsx does 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.

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-drive so how can it be a network issue?

Anybody ever experienced anything like this before?

解决方案

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.

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 = False is present in your code, you would get the standard 1004 error message when the file cannot be accessed (as in the example above).

这篇关于1004运行时错误,因为Excel找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆