zoukankan      html  css  js  c++  java
  • Office VBA进阶(-):遍历目录

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

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

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

    Syntax

    Dir[(pathname [, attributes] )]

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


    Sub SearchFolder(mypath As String, szExtension As String)
     
     
    ' 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
     
  • 相关阅读:
    编码问题
    僵尸进程与孤儿进程
    进程理论 阻塞非阻塞 同步异步 I/O操作
    浏览器上网流程以及套接字介绍
    OSI七层模型
    JsonResponse返回中文乱码问题
    查看源码所在位置
    linux ssh登录的小知识
    centos7安装python3.6
    Jquery的使用
  • 原文地址:https://www.cnblogs.com/buhaiqing/p/1340419.html
Copyright © 2011-2022 走看看