+ Reply to Thread
Results 1 to 7 of 7

VBA code to add folders and subfolders

  1. #1
    Registered User
    Join Date
    08-28-2012
    Location
    Alaska
    MS-Off Ver
    Excel 2007
    Posts
    3

    VBA code to add folders and subfolders

    Hello Everybody,

    I'm new to VBA and it has been fun using it on my spreadsheets, in between bouts of frustration!

    The project I've been tasked with is to create hundreds of new folders with 5 subfolders under each new folder.

    I would like to type the name of the new folder into cell C2, execute the code, and have the folders made for me.

    Here is where I'm at with the coding:

    Please Login or Register  to view this content.
    Actually, the code is working. It is just calling the folder name "[C2]" instead of what is IN cell C2

    Thanks for any help in advance!
    Last edited by rhlittau; 02-18-2013 at 07:54 PM. Reason: grammer

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: VBA code to add folders and subfolders

    Instead of

    Please Login or Register  to view this content.
    try

    Please Login or Register  to view this content.
    Ahmed

    P.S. Personally I do this using filesystemobject, but if this works for you then more power to you
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: VBA code to add folders and subfolders

    I have an alternative solution. Try this code to see if it works for you:

    Please Login or Register  to view this content.
    *******************************************************

    HELP WANTED! (Links to Forum threads)
    Trying to create reusable code for Custom Events at Workbook (not Application) level

    *******************************************************

  4. #4
    Registered User
    Join Date
    08-28-2012
    Location
    Alaska
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA code to add folders and subfolders

    Abousetta, Thanks for your reply, but I couldn't get your code to work. I was probably doing something wrong.(?)

    Mc84excel, I like your idea of the message box. It look good and allows me to do everything with the click of a button.

    Thanks for your help, problem solved!

  5. #5
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: VBA code to add folders and subfolders

    Quote Originally Posted by rhlittau View Post
    It look good and allows me to do everything with the click of a button.
    Glad to help.

    Just a few points with the code I posted:
    1. Using constant instead of string for the strNewYorkDirectoryPath is better practice. Faster. (I have provided updated code below)
    2. Should you ever change your directory path in future, you will only need to enter the new path into the code once rather than multiple times
    3. I didn't include a check on the folder name entered. (It will create issues if a users enter a slash at the end of the folder name or they enter an existing folder name)

    Please Login or Register  to view this content.
    Last edited by mc84excel; 02-20-2013 at 07:25 PM.

  6. #6
    Registered User
    Join Date
    08-28-2012
    Location
    Alaska
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA code to add folders and subfolders

    Thanks for explaining the "why" behind the code. As a newer person to VBA, "why" we do things is often lacking. It helps me to understand better. Thanks again!

  7. #7
    Valued Forum Contributor
    Join Date
    08-29-2012
    Location
    In lockdown
    MS-Off Ver
    Excel 2010 (2003 to 2016 but 2010 for choice)
    Posts
    1,766

    Re: VBA code to add folders and subfolders

    Quote Originally Posted by rhlittau View Post
    Thanks again!
    No problem.
    I was still worried about the backslash issue (point 3 on an earlier post). So I have adjusted the code to handle this. Now it will not matter whether the end user enters a backslash or not at the end of the folder name.
    (BTW if you make the sub public, you could then set up a button or keyboard shortcut to make it easier for the end users to run the code. Just a suggestion).

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1