2013-07-11 87 views
1

只是想知道......我有一段代碼 - 在excel中允許我在給定目錄中打開文件的宏。問題是我只想打開.xls文件,我打開的代碼打開了給定目錄中的所有文件。使用宏在給定的目錄中只打開.xls文件

任何人都可以幫助我解決這個問題。

謝謝。

Sub FindOpenFiles() 
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook 
Dim directory As String 

    directory = "O:\test" 

    Set FSO = CreateObject("Scripting.FileSystemObject") 
    Set folder = FSO.GetFolder(directory) 

    For Each file In folder.Files 
     Workbooks.Open file 
    Next file 
End Sub 

回答

2
Sub FindOpenFiles() 
Dim FSO As Scripting.FileSystemObject, folder As Scripting.folder, file As Scripting.file, wb As Workbook 
Dim directory As String 

    directory = "O:\test" 

    Set FSO = CreateObject("Scripting.FileSystemObject") 
    Set folder = FSO.GetFolder(directory) 


    For Each file In folder.Files 
     If Mid(file.Name, InStrRev(file.Name, ".") + 1) = "xls" Then 
      Workbooks.Open directory & Application.PathSeparator & file.Name 
     End If 
    Next file 
End Sub 

這炒菜很好...

0

試試這個,但我真的不知道
目錄=「的* .xls」

+0

嘗試已經......無論如何都不會工作...謝謝。 – Saint

1

看看這個工程,可能需要一個調整或兩個!

For Each file In folder.Files 
    If Right(file, 4) = ".xls" Then 
     Workbooks.Open file 
    End If 
Next file 
+0

謝謝你......用了同樣的想法...... – Saint

+0

輝煌,很高興看到你得到你需要的東西!此代碼是否按原樣工作? – Conquistador

+0

是的,完美的作品...是否我需要..快樂的日子:) – Saint

相關問題