我試圖循環並打開名爲(BU)的文件夾中的所有文件,該文件夾位於與我的宏所在工作表相同的目錄中。我能夠看到myfile正確獲取第一個文件名,但是當工作簿嘗試打開時,我得到運行時錯誤1004。任何幫助,將不勝感激。VBA-試圖打開文件夾中的所有工作簿
Sub LoopAndOpen()
Dim myfile As String, Sep As String, stringA As String, path1 As String
Sep = Application.PathSeparator
path1 = ActiveWorkbook.Path & Sep & "BU" & Sep
myfile = Dir(path1 & "*.xlsm")
Do While myfile <> ""
Workbooks.Open myfile
myfile = Dir()
Loop
End Sub
編輯:我結束了使用Unicco的過程,它的工作完美。
這工作完美。非常感謝! – user3276264
+ 1爲另一種方法。提示:對於所有四種格式的.xls,.xlsx和.xlsm以及.xlsb,您可以使用If InStr(objFile,「.xls」):) –