创建文件夹:
Sub CreateFolder()
Dim fs As Object
Dim folderPath As String
' 文件夹路径
folderPath = "C:\Path\To\Your\Folder"
' 创建FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
' 检查文件夹是否存在
If Not fs.FolderExists(folderPath) Then
' 如果文件夹不存在,则创建
fs.CreateFolder folderPath
MsgBox "Folder created successfully!"
Else
MsgBox "Folder already exists!"
End If
End Sub
删除文件夹:
Sub DeleteFolder()
Dim fs As Object
Dim folderPath As String
' 文件夹路径
folderPath = "C:\Path\To\Your\Folder"
' 创建FileSystemObject
Set fs = CreateObject("Scripting.FileSystemObject")
' 检查文件夹是否存在
If fs.FolderExists(folderPath) Then
' 如果文件夹存在,则删除
fs.DeleteFolder folderPath
MsgBox "Folder deleted successfully!"
Else
MsgBox "Folder does not exist!"
End If
End Sub
在使用这些代码之前,请确保替换folderPath为你实际的文件夹路径。同时,确保在进行文件夹删除操作时小心谨慎,因为这是一个不可逆操作,会永久删除文件夹及其内容。
这些示例演示了如何使用VBA操纵文件夹。
转载请注明出处:http://www.zyzy.cn/article/detail/6699/VBA