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

时间:2013-03-06 18:19:34

标签: excel vba

我正在尝试在少数excel表上执行一些程序并使用以下代码。我收到 Application.Workbooks.Open(StrFile)的运行时错误1004。好像它无法找到该文件。我认为这是因为它没有显示文件路径,而只是显示在屏幕截图中显示的目录中的文件:

image of where error occurs

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

1 个答案:

答案 0 :(得分:1)

改为使用:

Application.Workbooks.Open(FilePath & strFile)