简体   繁体   English

循环打开Excel工作表时出现VBA错误

[英]VBA Error While Opening Excel Worksheet in a Loop

I am trying to perform few procedure on few excel sheets and using the following code. 我试图在几个Excel工作表上执行一些步骤,并使用以下代码。 I am getting a RunTime Error 1004 for Application.Workbooks.Open (StrFile) . 我收到Application.Workbooks.Open(StrFile)的运行时错误1004。 It seems like it is not able to find the file. 似乎无法找到该文件。 The reason why I think that is because it is not showing the filepath but just the file in the directory shown below in the screenshot: 我之所以这么认为,是因为它没有显示文件路径,而只是显示了屏幕截图中所示的目录中的文件:

发生错误的位置的图像

Sub ExcelerFinal()

Dim FileCount As Integer
Dim FileName As String, FileNameGIS As String
Dim FilePath As String, StrFile As String

FileCount = 0
FileName = "MyFile_" + CStr(FileCount)
FileNameGIS = "MyFileGIS_" + CStr(FileCount)

FilePath = "E:\Database Project\ACS Estimate 2011\LoopTest\Test_RawData\"

StrFile = Dir(FilePath & "*.xlsx")

While StrFile <> ""
    Application.Workbooks.Open (StrFile) 'Error occurs on this line 
    FileCount = FileCount + 1
    FileName = "MyFile_" + CStr(FileCount)
    FileNameGIS = "MyFileGIS_" + CStr(FileCount)

    ' Lots of code to manipulate file
    ' unrelated to issue
    Windows(FileNameGIS).Close
    StrFile = Dir
Wend
End Sub

改用:

Application.Workbooks.Open(FilePath & strFile)

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

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