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. When you are running please keep in mind 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 use VBA Macro Code

Here are the instructions to use above macro in Visual basic editor.

  • Open Visual Basic Editor(VBE) by clicking Alt +F11
  • Go to code window by clicking F7
  • Copy above specified macro or procedure
  • Paste above copied code in code window
  • Run macro by clicking F5 or Run command
  • Now you can find new folder with Today’s Date(3rd October 2019) in the specified folder.

Related Reference Articles

You can also learn complete details like syntax, example and etc by clicking on the following buttons.

VBA File and Directory Functions in Excel Related Posts

Leave a Comment

Your email address will not be published. Required fields are marked *