Solved

How do I create folders within a folder in Excel VBA ?

Posted on 2013-11-22
3
417 Views
Last Modified: 2013-11-22
Hi,

I have an Excel 2010 VBA application in which I create a new empty folder ("\Training") on my C: drive. This works successfully.

However, I want to also create 2 empty subfolders within the \Training folder but I'm not sure how to do this.

Further, how do I check MKDIR to ensure it has successfully created the folders ?

This is my code thus far ...

   Dim MyPath As String
       
   MyPath = Environ("USERPROFILE") & "\Training"

   If Len(Dir(MyPath, vbDirectory)) = 0 Then
      MkDir MyPath
      ' Create a '\Booking' folder  and a '\Finance' folder inside the '\Training' folder
      MsgBox "A new Windows folder with subfolders has been created successfully", vbInformation
   End If


Thanks for your help
Toco
0
Comment
Question by:Tocogroup
3 Comments
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39668606
Hi,

Pls try

   Dim MyPath As String, MyPathB As String, MyPathF As String
       
   MyPath = Environ("USERPROFILE") & "\Training"

   If Len(Dir(MyPath, vbDirectory)) = 0 Then
      MkDir MyPath
      MyPathB = Environ("USERPROFILE") & "\Training\Booking"
      If Len(Dir(MyPathB, vbDirectory)) = 0 Then
        MkDir MyPathB
      End If
      MyPathF = Environ("USERPROFILE") & "\Training\Finance"
      If Len(Dir(MyPathF, vbDirectory)) = 0 Then
        MkDir MyPathF
      End If
      ' Create a '\Booking' folder  and a '\Finance' folder inside the '\Training' folder
      MsgBox "A new Windows folder with subfolders has been created successfully", vbInformation
   End If

Open in new window

Regards
0
 
LVL 6

Expert Comment

by:Ramakrishna Prabhu
ID: 39668609
How many subfolders you want to create?

Will they remain constant or dynamic?
0
 

Author Closing Comment

by:Tocogroup
ID: 39668657
Many thanks for that. It worked a treat.
Toco
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question