如何读取excel VBA目录中的某些文件 [英] how to read certain files in a directory in excel VBA
问题描述
以下是一个示例:
目录: c:\temp
文件格式: xxxxx0123.xls
(xxxxx表示文件名)。
我尝试使用 Application.FileSearch
,但它不会在Excel 2007中工作。有没有人有好的建议?
感谢提前
您可以使用 DIR
来查找与您的模式,即此代码打开这些文件,抓住他们的路径,并再次关闭文件
如果您需要查看子文件夹
p> Sub GetFiles()
Dim strFolder As String
Dim strFileName As String
Dim wb As Workbook
strFolder =C:\temp
strFileName = Dir(strFolder&\ * 123.xls)
Do While Len(strFileName)> 0
设置wb = Workbooks.Open(strFileName)
Debug.Print wb.FullName
wb.Close False
strFileName =目录
循环
结束子
I want to read certain excel files from a directory and then open them in excel-2007 with VBA.
Here is an example:
directory: c:\temp
file pattern: is xxxxx0123.xls
(xxxxx represents the file names).
I try to use Application.FileSearch
, but it won't work in Excel 2007. Does any one have good suggestions?
Thanks in advance
You can use DIR
to find files matching your pattern, ie this code opens these files, grabs their path, and closes the files again
The code can be made recursive if you need to look in sub-folders
Sub GetFiles()
Dim strFolder As String
Dim strFileName As String
Dim wb As Workbook
strFolder = "C:\temp"
strFileName = Dir(strFolder & "\*123.xls")
Do While Len(strFileName) > 0
Set wb = Workbooks.Open(strFileName)
Debug.Print wb.FullName
wb.Close False
strFileName = Dir
Loop
End Sub
这篇关于如何读取excel VBA目录中的某些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!