C:\\Vishwa\\MyFolders<\/strong> not found then, folder will not be created and this VBA function will throw an error (Path Not Found)\n<\/p><\/blockquote>\nHow to create a Folder in Windows using VBA <\/h3>\n As explained above, I have created a function which will create a folder inside a root directory specified.<\/p>\n
\n<\/code><\/pre>\nNote: <\/h2>\n As you can see that it might be possible that you may give a path name which is already existing and then it will lead to an error. So before creating a folder, how do we make sure if this path already exists or not? <\/p>\n
How to check if a directory is already existing ? <\/h3>\n For the above CreateFolder Function to create a folder successfully there are two conditions which should be met: \n1 . rootDirectory = “C:\\Vishwa\\MyFolders\\” should be existing in windows. \n2 . folderToBeCreated = “MyFolder1” should not be existing inside the rootDirectory already. \n<\/strong><\/p>\nTo check this you can use another VBA function called Dir(pathName<\/i>, vbDirectory) as String <\/strong> <\/p>\nWhat will below statement do? <\/h3>\n Dir(rootDirectory , vbDirectory)<\/strong><\/p>\nIn the above example, rootDirectory = “C:\\Vishwa\\MyFolders\\”. \nThis function will return the name of the child folder i.e. MyFolders<\/strong> in the above directory full path if and only if<\/strong> this is an existing path in windows.<\/p>\nTherefore by below code you can make sure that rootFolderPath is existing and FolderName does not exists in the root folder before trying to create a new folder inside that.<\/p>\n
\n ' Check the root directory and folder path\n ' before creating it directly\n If Len(Dir(rootDirectory, vbDirectory)) <> 0 Then 'check if RootDirectory Exists?\n If Len(Dir(path, vbDirectory)) = 0 Then ' full path should not exist already\n VBA.MkDir (path) \n MsgBox \"Folder is created successfully\"\n Else\n MsgBox \"Folder is already existing in the root directory\"\n End If\n Else\n MsgBox \"Root directory does not exist\"\n End If\n<\/code><\/pre>\nTherefore your complete code for creating a folder inside a directory will look like below which will give you correct error message:<\/p>\n
\n Sub CreateFolder()\n Dim rootDirectory As String\n Dim folderToBeCreated As String\n Dim path As String\n ' Set the root directory path\n ' where you want to create\n ' your folder\n rootDirectory = \"C:\\Vishwa\\MyFolders\"\n ' give a valid name for your folder\n folderToBeCreated = \"MyFolder1\"\n ' Path for MkDir VBA function\n ' would be the concatination\n ' of above two\n path = rootDirectory & folderToBeCreated\n ' Check the root directory and folder path\n ' before creating it directly\n If Len(Dir(rootDirectory, vbDirectory)) <> 0 Then 'check if RootDirectory Exists?\n If Len(Dir(path, vbDirectory)) = 0 Then ' full path should not exist already\n VBA.MkDir (path) ' or VBA.MkDir (\"C:\\Vishwa\\MyFolders\\MyFolder1\")\n MsgBox \"Folder is created successfully\"\n Else\n MsgBox \"Folder is already existing in the root directory\"\n End If\n Else\n MsgBox \"Root directory does not exist\"\n End If\nEnd Sub\n<\/code><\/pre>\n<\/span>","protected":false},"excerpt":{"rendered":"Dear LEM Readers, Till now, I had published many articles on how to list files from a folder \/ sub folder,<\/p>\n","protected":false},"author":45,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[1246,1679,1675],"tags":[],"class_list":["post-4781","post","type-post","status-publish","format-standard","hentry","category-macro","category-excel-macro-beginner","category-excel-macro-for-beginners"],"yoast_head":"\n
How to create directory \/ folder using Excel VBA<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n