[英]excel Method 'open' of object 'workbooks' failed error when opening a read only workbook
I have code that should open open a handfull of workbooks, pull key info and close them. 我的代码应该打开一本完整的工作簿,提取关键信息并关闭它们。 i can see the workbook loading but then when it is about to open i get the runtime error 1004 that says
我可以看到工作簿正在加载,但是当它即将打开时,出现运行时错误1004
Method 'open' of object 'workbooks' failed
对象“工作簿”的方法“打开”失败
My code is as follows and when i debug it takes me to the 2nd line under the do while statement: 我的代码如下,当我调试时,将我带到do while语句下的第二行:
Sub OEEsummmary()
Dim Gcell As Range
Dim MySheet As Worksheet
Dim Txt$, MyPath$, MyWB$
Dim myValue As Integer
Dim x As Long
Dim v As Variant, r As Range, rWhere As Range
MyPath = "L:\Manufacturing Engineering\Samuel Hatcher\"
x = 2
Set MySheet = ActiveSheet
Application.ScreenUpdating = False
Do While MySheet.Range("A" & x).Value <> ""
MyWB = MySheet.Range("A" & x).Text
Workbooks.Open Filename:=MyPath & MyWB, ReadOnly:=True, IgnoreReadOnlyRecommended:=True
Set Gcell = ActiveSheet.Range("E21")
With MySheet.Range("A" & x)
.Offset(0, 7).Value = Gcell.Value
End With
ActiveWorkbook.Close savechanges:=False
x = x + 1
Loop
End Sub
I tried to change the different defined variable to variant as per instructions of other people who had the same issue but nothing worked. 我试图按照其他遇到相同问题但无济于事的人的指示,将不同的定义变量更改为variant。 Any help is greatly appreciated thanks!
任何帮助,万分感谢!
update* I moved the two file names that were an issue to the bottom of the list and every other file name opened and copied the info perfectly but when the loop got to the last two files it gives me that error. 更新*我将两个有问题的文件名移到列表的底部,其他所有文件名均打开并完美复制了信息,但是当循环到达最后两个文件时,出现了这个错误。 all of the files are manipulated copies of the bottom 2 so i dont see why it doesnt work
所有文件都是底部2的可操纵副本,所以我不明白为什么它不起作用
update2* it seems as though the only workbooks that give the error and wont load are the ones open on another computer in the network, when this program is run all the workbooks will be open on other computers update2 *似乎唯一出现错误且不会加载的工作簿是在网络中另一台计算机上打开的工作簿,运行此程序时,所有工作簿都将在其他计算机上打开
it turns out a few of the files were corrupted so when the code encountered this it didnt work. 事实证明,其中一些文件已损坏,因此当代码遇到此问题时,它不起作用。 the solution to this problem was to close the corrupted workbook then when I reopened it I clicked the arrow next to open and selected "open and repair" then saved a new copy of the file and the code ran smoothly
解决此问题的方法是关闭损坏的工作簿,然后当我重新打开它时,单击打开旁边的箭头并选择“打开并修复”,然后保存文件的新副本,代码顺利运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.