2015-06-14 180 views
0

我的代码是:Excel VBA中打开的工作簿

Dim wb As Workbook 
Dim MyObj As Object, MySource As Object, file As Variant 
file = Dir("C:\Users\dlf164\Desktop\NE\") 
While (file <> "") 
    If InStr(file, a) > 0 Then 
     Set wb = Workbooks.Open(file) 

    End If 
file = Dir 
Wend 

其中我收到的错误是应用或对象定义运行时错误

如何解决这个问题?

+0

也许'文件'不是Excel(兼容)文件? –

+0

它的excel兼容 – user3764484

+0

http://stackoverflow.com/questions/10380312/loop-through-files-in-a-folder-using-vba –

回答

2

Dir()只返回文件名,但Workbooks.Open()需要完整路径。尝试这样的:

Dim wb As Workbook 
Dim MyObj As Object, MySource As Object, file As Variant 

Dim path As String 
path = "C:\Users\dlf164\Desktop\NE\" 
file = Dir(path) 
While (file <> "") 
    If InStr(file, a) > 0 Then 
     Set wb = Workbooks.Open(path & file) 
    End If 
file = Dir 
Wend 
+0

感谢它现在工作 – user3764484

相关问题