VBA如何删除包含特定名称的文件夹

时间:2017-05-09 14:06:30

标签: vba excel-vba excel

如果我在文件夹(子文件夹)中有一个文件夹。 子文件夹名称包含单词“复制”。 如何在Excel中编码以删除包含此单词的所有子文件夹?

我已经管理了这么远,但它删除了所有子文件夹。

Private Sub CommandButton1_Click()
    Dim FSO As Object
    Dim oPath As String

    Set FSO = CreateObject("scripting.filesystemobject")
    oPath = Blad1.Cells(2, 4).Value

    If Right(oPath, 1) = "\" Then
        oPath = Left(oPath, Len(oPath) - 1)
    End If

    If FSO.FolderExists(oPath) = False Then
        MsgBox oPath & " doesn't exist"
        Exit Sub
    End If

    On Error Resume Next
        'Delete files
        'FSO.deletefile oPath & "\*.*", True
        'Delete subfolders
        FSO.deletefolder oPath & "\*.*", True
    On Error GoTo 
End Sub

0 个答案:

没有答案
相关问题