如何读取excel VBA目录中的某些文件 [英] how to read certain files in a directory in excel VBA

查看:106
本文介绍了如何读取excel VBA目录中的某些文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个目录中读取某些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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆