只是想知道......我有一段代码 - excel中的一个宏,允许我打开给定目录中的文件。问题是我只想打开.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
答案 0 :(得分: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
这很好......
答案 1 :(得分:1)
看看这是否有效,可能需要一两个调整!
For Each file In folder.Files
If Right(file, 4) = ".xls" Then
Workbooks.Open file
End If
Next file
答案 2 :(得分:0)
试试这个,但我不太确定
directory =“* .xls”