简体   繁体   English

如何在excel VBA中读取目录中的某些文件

[英]how to read certain files in a directory in excel VBA

I want to read certain excel files from a directory and then open them in with VBA. 我想从目录中读取某些Excel文件,然后使用VBA在打开它们。

Here is an example: 这是一个例子:
directory: c:\\temp 目录: c:\\temp
file pattern: is xxxxx0123.xls (xxxxx represents the file names). 文件模式: xxxxx0123.xls (xxxxx代表文件名)。

I try to use Application.FileSearch , but it won't work in Excel 2007. Does any one have good suggestions? 我尝试使用Application.FileSearch ,但在Excel 2007中将无法使用。有人建议吗?

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 您可以使用DIR查找与模式匹配的文件,即此代码打开这些文件,获取它们的路径,然后再次关闭这些文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM