VBA 遍历某个文件夹指定文件类型

该文章详细描述了一个使用VBA编写的Sub过程,通过FileSystemObject在给定路径下的文件夹中递归查找MicrosoftExcel97-2003工作表或工作表类型的文件,对这些文件进行特定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

时间:2016-10-10 10:14:00

Private Sub 遍历_Click()
    Application.ScreenUpdating = False
    Dim fso, folder, fds, fd, folder1, folder2, fs, f
    Dim dirpath, atdirname, atfilename As String, arr
    Dim putrows As Integer
    dirpath = ThisWorkbook.Path
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder1 = fso.GetFolder(dirpath)  '获得文件夹
    Set fds = folder1.SubFolders        '子文件夹集合
    putrows = 5
    For Each fd In fds                  '遍历子文件夹
         
        'Debug.Print fd.Name
        If fd.Name = "目录" Then
            atdirname = fd.Name
            Set folder2 = fd                '获得文件夹2
            Set fs = folder2.Files          '文件集合
            For Each f In fs                '遍历文件
                 
                If f.Type = "Microsoft Excel 97-2003 工作表" Or f.Type = "Microsoft Excel 工作表" Then
                    '是表格文件
                    'Debug.Print f.Name
                    '执行其他操作.....
                End If
            Next
            'Debug.Print
            'putrows = putrows + 1
         End If
    Next
End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值