VBA Create Folder with Today’s or current Date. Today’s date is 3rd Oct 2019. We are checking specified folder is already exists in the specified folder. If it is not available then we are creating new folder using MkDir VBA function. The folder name always displays system current date. We use VBA Now function to get current date. In this tutorial we have explained examples and step by step instructions for your reference.
Let us see an example macro VBA Create Folder with Today’s or current system Date. Please keep in mind this post created on 3rd Oct 2019. It displays output according to today’s date. We use FolderExists method and FSO object. Where FSO represents the FileSystemObject in VBA. MkDir helps to create new folder in VBA. In the below example Now VBA function helps us to find current or today’s date. You can change format as per your requirement.
'VBA Create Folder with Today's or current Date 'Note: I have created this post on 3rd Oct 2019(It displays output as per this date) Sub VBAF1_Create_Folder_with_Todays_Date() 'Variable declaration Dim sFolderName As String, sFolder As String Dim sFolderPath As String 'Main Folder sFolder = "C:\VBAF1\" 'Folder Name sFolderName = Format(Now, "dd MMM yyyy") 'Folder Path sFolderPath = "C:\VBAF1\" & sFolderName 'Create FSO Object Set oFSO = CreateObject("Scripting.FileSystemObject") 'Check Specified Folder exists or not If oFSO.FolderExists(sFolderPath) Then 'If folder is available with today's date MsgBox "Folder already exists with today's date!", vbInformation, "VBAF1" Exit Sub Else 'Create Folder MkDir sFolderPath 'Display message on the screen MsgBox "Folder has created with today's date: " & vbCrLf & vbCrLf & sFolderPath, vbInformation, "VBAF1" End If End Sub
Output: You can find following output screenshot for your reference. The date might change when you are running above VBA macro code. When you are running please keep in mind folder creates with current date.
If we are not checking folder is exists or not, then it throws following error when folder is already exists.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.