今天想讲解如何用VBA宏得到一个目录下以某个后缀名结尾的文件。

这要用到一个叫Dir的函数:

第一个参数就是所要遍历的目录路径,第二个参数是可选的,就是不是必要的。它是罗列了一些查找的属性

Syntax

Dir[(pathname [, attributes] )]

 

还是举个例子说说吧,现在我想得到C:\test\demo目录下的所有Excel 文件该如何作呢?

)
 
 ' Add a slash at the end of the path if needed.
    If Right(mypath, 1<> "\" Then
         mypath 
= mypath & "\"
    
End If
    
    
  
' If there are no Excel files in the folder, exit.
    FilesInPath = Dir(mypath & szExtension)
    
If FilesInPath = "" Then
        
MsgBox "No files found"
        
Exit Sub
    
End If

    
' Fill the myFiles array with the list of Excel files
    ' in the search folder.
    FNum = 0
    
Do While FilesInPath <> ""
        FNum 
= FNum + 1
        
ReDim Preserve MyFiles(1 To FNum)
        MyFiles(FNum) 
= FilesInPath
        FilesInPath 
= Dir()
    
Loop
End Sub
 

 

相关文章:

  • 2021-08-16
  • 2021-10-21
  • 2022-01-22
  • 2021-08-26
  • 2021-08-19
  • 2021-10-17
  • 2021-05-25
  • 2021-10-13
猜你喜欢
  • 2022-12-23
  • 2021-06-02
  • 2021-08-10
  • 2021-12-31
  • 2022-01-09
  • 2021-12-03
  • 2021-10-31
相关资源
相似解决方案