VBA Create Folder with Today’s Date

VBA Create Folder with Today's Date

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.

VBA Create Folder with Today’s or Current Date

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.
VBA Create Folder with Today's Date

If we are not checking folder is exists or not, then it throws following error when folder is already exists.
File or Path access error

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Leave a Comment