简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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