Excel交流网
  • 设为首页|收藏本站|手机版
  • Excel-教程-技巧-培训视频

  • 网站首页
  • Excel教程
  • 关于我们
  • 新闻动态
  • Excel资源
  • 招贤纳士

Excel教程

Excel操作
Excel函数
Excel图表
Excel VBA
Excel 行业应用

联系方式

Excel中国 联系方式
电  话:400-855-3990
邮  编:528400
Email:support@zstm.com
网  址:www.excel-cn.com

当前位置:网站首页 > Excel教程 > Excel VBA
Excel VBA

excel vba删除所有空白文件夹

我们为了整理文件,我们都会建立文件夹。随着时间的推移,很多文件夹的文件可能被移出,有很多空白的文件夹

如何用Excel vba删除空白的文件夹呢?我们用excel vba 创建自定义的函数


删除空白文件夹的函数:

Sub DelEmtyDir(ByVal strPath As String)
    Dim fso As New FileSystemObject
    Dim strDirName As String, LastDir As String
    Dim strFld As String, fld As Folder
    If strPath = "Fase" Or strPath = "" Then Exit Sub
    If Right(strPath, 1) <> "" Then strPath = strPath & ""
    strDirName = Dir(strPath, vbDirectory) '取得子文件夹
    Do While strDirName <> ""
        If strDirName <> "." And strDirName <> ".." Then
            If (GetAttr(strPath & strDirName) And vbDirectory) = vbDirectory Then
                LastDir = strDirName
                Set fld = fso.GetFolder(strPath & strDirName)
                If fld.Size = 0 Then
                    fld.Delete
                    strFld = Left(strPath & strDirName, InStrRev(strPath$ & strDirName, "") - 1)
                    Call DelEmtyDir(strFld)
                Else
                    Call DelEmtyDir(strPath & strDirName)
                End If
                strDirName = Dir(strPath, vbDirectory)
                Do Until strDirName = LastDir Or strDirName = ""
                    strDirName = Dir
                Loop
                If strDirName = "" Then Exit Do
            End If
            strDirName = Dir
        End If
    Loop
    Set fso = Nothing
End Sub


运行函数,填写删除的文件夹的路径

Sub 删除空文件夹()
    Dim strPath As String
    strPath = Application.InputBox("请输入文件夹名称", "输入文件夹名称", ThisWorkbook.Path, 2)
    If strPath = "Fase" Or strPath = "" Then Exit Sub
    Call DelEmtyDir(strPath)
End Sub


运行效果图:

blob.png



参考至:小智雅汇(头条号)




点击次数:  更新时间:2017-11-18 16:57:35  【打印此页】  【关闭】
上一条:​ 删除excel表格中的页面虚线  下一条:excel vba获取子文件夹名称,文件夹大小
本站动态|在线留言|在线反馈|友情链接|会员中心|站内搜索|网站地图|网站管理

中山市天鸣科技发展有限公司 版权所有 1999-2020 粤ICP备10043721号

广东省中山市西苑广场富贵阁 528400

QQ:4008553990 电话:0760-88315075

Excel交流网主要交流Excel教程、Excel技巧、Excel培训、Excel函数公式、Excel图表以及Excel VBA,为网友提供一个最全的Excel交流网站

Excel教程|Excel技巧|Excel培训|Excel函数公式|Excel图表|VBA

Powered by MetInfo 5.3.12 ©2008-2022  www.metinfo.cn