Wednesday, April 27, 2016

How to create new folder at runtime using excel VBA?

Maintain folder structure is a vital task in  automation projects. Sometimes, it is very essential to delete and create folders at runtime. to achieve this task user needs to add a code which will be create folder at runtime.

Since to create specific folder at runtime, user need to automate this task in excel macro itself. Automating new folder creation task not only maintain folder structure but also ensures quality of work if there are several several folders to be deleted and create at runtime.

Here is the code which can be used to create new folder on specific path:

Sub Create_Folder()
' Author: Dreams24
' Written for VBA Tricks and tips blog
' https://vbatricksntips.com

Dim fso
Dim fPath As String
Dim fName As String

' Set folder path where new folder to be created

fPath = "C:\Users\Dreams24\Desktop"

' Assign name of folder which is to be created
fName = "Test"

Set fso = CreateObject("Scripting.FileSystemObject")

    If Not fso.FolderExists(fPath & "\" & fName) Then
        fso.CreateFolder (fPath & "\" & fName)
    Else
        MsgBox "Folder is already exists", vbInformation
    End If

End Sub

No comments: