vba怎么新建文件夹并以日期命名
时间: 2024-09-16 16:00:35 浏览: 98
在VBA (Visual Basic for Applications) 中,你可以使用 `FileSystemObject` 对象来创建新的文件夹,并通过当前日期进行命名。以下是一个简单的示例代码,展示如何在指定路径下创建一个新的文件夹,其名称包含今天的日期:
```vba
Sub CreateFolderWithDate()
Dim fso As Object ' FileSystemObject
Dim folderPath As String
Dim currentDate As Date
Dim newFolderPath As String
Set fso = CreateObject("Scripting.FileSystemObject") ' 创建 FileSystemObject
' 指定文件夹将要创建的路径
folderPath = "C:\Your_Directory" ' 将 "Your_Directory" 替换为你实际需要的目录路径
' 获取当前日期并格式化
currentDate = Date
newFolderPath = folderPath & "\" & Format(currentDate, "yyyy-mm-dd") ' yyyy-mm-dd 格式例如 2023-03-15
' 创建新文件夹
If Not fso.FolderExists(newFolderPath) Then
fso.CreateFolder newFolderPath ' 如果文件夹不存在则创建
MsgBox "Folder created successfully: " & newFolderPath, vbInformation
Else
MsgBox "Folder already exists at this path.", vbInformation
End If
Set fso = Nothing ' 清理内存
End Sub
```
阅读全文