{"id":4781,"date":"2016-11-23T06:56:05","date_gmt":"2016-11-23T06:56:05","guid":{"rendered":"http:\/\/www.learnexcelmacro.com\/wp\/?p=4781"},"modified":"2016-11-23T06:56:05","modified_gmt":"2016-11-23T06:56:05","slug":"how-to-create-folders-in-windows-via-excel-vba","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/","title":{"rendered":"How to create folders in windows via Excel VBA"},"content":{"rendered":"

Dear LEM Readers,
\nTill now, I had published many articles on how to list files from a folder \/ sub folder<\/a>, how to list folders and sub folders <\/a> etc. You get a FREE excel workbook as well to play around.
\nIn both above articles, You have learnt playing around an existing folders or files in folders. Then I thought of publishing an article where I can teach you how to CREATE a folder in windows using excel programming. There is a very simple VBA function which enables you to create a folder in windows:
\n

\"Create<\/a>

Create folder – VBA code<\/p><\/div><\/p>\n

MkDir(Path<\/i> as String)<\/p>\n

Where:<\/strong>
\nPath : This is the full path of folder which has to be created.<\/p>\n

Example: MkDir(“C:\\Vishwa\\MyFolders\\Folder1”)<\/p>\n

In the above example, MkDir will first look for this Directory – C:\\Vishwa\\MyFolders<\/strong> and then create a folder named “Folder1” inside that.
\nNote: If root directory i.e. 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>\n

How 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>\n

Note: <\/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>\n

To check this you can use another VBA function called Dir(pathName<\/i>, vbDirectory) as String <\/strong> <\/p>\n

What will below statement do? <\/h3>\n

Dir(rootDirectory , vbDirectory)<\/strong><\/p>\n

In 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>\n

Therefore 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>\n

Therefore 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":[],"yoast_head":"\nHow to create directory \/ folder using Excel VBA<\/title>\n<meta name=\"description\" content=\"Create folders and sub folders using excel VBA code. How to Check if folder path already exists. Create a folder inside a directory using Excel Macro. Macro to create a folder\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to create folders in windows via Excel VBA\" \/>\n<meta property=\"og:description\" content=\"Create folders and sub folders using excel VBA code. How to Check if folder path already exists. Create a folder inside a directory using Excel Macro. Macro to create a folder\" \/>\n<meta property=\"og:url\" content=\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\" \/>\n<meta property=\"og:site_name\" content=\"Let's excel in Excel\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:author\" content=\"http:\/\/www.facebook.com\/vmlogger\" \/>\n<meta property=\"article:published_time\" content=\"2016-11-23T06:56:05+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/learnexcelmacro.com\/wp\/wp-content\/uploads\/sites\/11\/2016\/11\/createFolder.jpg\" \/>\n<meta name=\"author\" content=\"Vishwamitra Mishra\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/www.twitter.com\/learnexcelmacro\" \/>\n<meta name=\"twitter:site\" content=\"@learnexcelmacro\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vishwamitra Mishra\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\"},\"author\":{\"name\":\"Vishwamitra Mishra\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"headline\":\"How to create folders in windows via Excel VBA\",\"datePublished\":\"2016-11-23T06:56:05+00:00\",\"dateModified\":\"2016-11-23T06:56:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\"},\"wordCount\":29,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"articleSection\":[\"Excel Macro\",\"Excel Macro Beginner\",\"Excel Macro Tutorial\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\",\"name\":\"How to create directory \/ folder using Excel VBA\",\"isPartOf\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\"},\"datePublished\":\"2016-11-23T06:56:05+00:00\",\"dateModified\":\"2016-11-23T06:56:05+00:00\",\"description\":\"Create folders and sub folders using excel VBA code. How to Check if folder path already exists. Create a folder inside a directory using Excel Macro. Macro to create a folder\",\"breadcrumb\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/vmlogger.com\/excel\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel Macro\",\"item\":\"https:\/\/vmlogger.com\/excel\/macro\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"How to create folders in windows via Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#website\",\"url\":\"https:\/\/vmlogger.com\/excel\/\",\"name\":\"Let's excel in Excel\",\"description\":\"Let's share knowledge\",\"publisher\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/vmlogger.com\/excel\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5\",\"name\":\"Vishwamitra Mishra\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"contentUrl\":\"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png\",\"width\":528,\"height\":560,\"caption\":\"Vishwamitra Mishra\"},\"logo\":{\"@id\":\"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/\"},\"description\":\"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.\",\"sameAs\":[\"http:\/\/www.learnexcelmacro.com\",\"http:\/\/www.facebook.com\/vmlogger\",\"https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro\",\"https:\/\/www.youtube.com\/c\/VMLogger\"],\"url\":\"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to create directory \/ folder using Excel VBA","description":"Create folders and sub folders using excel VBA code. How to Check if folder path already exists. Create a folder inside a directory using Excel Macro. Macro to create a folder","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/","og_locale":"en_US","og_type":"article","og_title":"How to create folders in windows via Excel VBA","og_description":"Create folders and sub folders using excel VBA code. How to Check if folder path already exists. Create a folder inside a directory using Excel Macro. Macro to create a folder","og_url":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/","og_site_name":"Let's excel in Excel","article_publisher":"http:\/\/www.facebook.com\/vmlogger","article_author":"http:\/\/www.facebook.com\/vmlogger","article_published_time":"2016-11-23T06:56:05+00:00","og_image":[{"url":"http:\/\/learnexcelmacro.com\/wp\/wp-content\/uploads\/sites\/11\/2016\/11\/createFolder.jpg"}],"author":"Vishwamitra Mishra","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/www.twitter.com\/learnexcelmacro","twitter_site":"@learnexcelmacro","twitter_misc":{"Written by":"Vishwamitra Mishra"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#article","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/"},"author":{"name":"Vishwamitra Mishra","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"headline":"How to create folders in windows via Excel VBA","datePublished":"2016-11-23T06:56:05+00:00","dateModified":"2016-11-23T06:56:05+00:00","mainEntityOfPage":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/"},"wordCount":29,"commentCount":4,"publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"articleSection":["Excel Macro","Excel Macro Beginner","Excel Macro Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/","url":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/","name":"How to create directory \/ folder using Excel VBA","isPartOf":{"@id":"https:\/\/vmlogger.com\/excel\/#website"},"datePublished":"2016-11-23T06:56:05+00:00","dateModified":"2016-11-23T06:56:05+00:00","description":"Create folders and sub folders using excel VBA code. How to Check if folder path already exists. Create a folder inside a directory using Excel Macro. Macro to create a folder","breadcrumb":{"@id":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/vmlogger.com\/excel\/2016\/11\/how-to-create-folders-in-windows-via-excel-vba\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/vmlogger.com\/excel\/"},{"@type":"ListItem","position":2,"name":"Excel Macro","item":"https:\/\/vmlogger.com\/excel\/macro\/"},{"@type":"ListItem","position":3,"name":"How to create folders in windows via Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/vmlogger.com\/excel\/#website","url":"https:\/\/vmlogger.com\/excel\/","name":"Let's excel in Excel","description":"Let's share knowledge","publisher":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/vmlogger.com\/excel\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/7500a107b0b2d35a8492acf0d11fc8e5","name":"Vishwamitra Mishra","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/","url":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","contentUrl":"https:\/\/vmlogger.com\/excel\/wp-content\/uploads\/sites\/11\/2022\/07\/avataaars-1.png","width":528,"height":560,"caption":"Vishwamitra Mishra"},"logo":{"@id":"https:\/\/vmlogger.com\/excel\/#\/schema\/person\/image\/"},"description":"My name is Vishwamitra Mishra. Friends Call me Vishwa. I hold a Bachelor\u2019s Degree in Computer Science from D.A.V.V. Indore & currently working as a Technical Lead having over 7 years of experience.","sameAs":["http:\/\/www.learnexcelmacro.com","http:\/\/www.facebook.com\/vmlogger","https:\/\/twitter.com\/https:\/\/www.twitter.com\/learnexcelmacro","https:\/\/www.youtube.com\/c\/VMLogger"],"url":"https:\/\/vmlogger.com\/excel\/author\/vishwamitra\/"}]}},"_links":{"self":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/4781"}],"collection":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/comments?post=4781"}],"version-history":[{"count":0,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/posts\/4781\/revisions"}],"wp:attachment":[{"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/media?parent=4781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/categories?post=4781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vmlogger.com\/excel\/wp-json\/wp\/v2\/tags?post=4781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}