Create Sub Directories From Cell Values

  • Create sub directories from Cell Values


    I Need a VB macro to Create sub directories unless sub directory already exist. My file will be Save 5 Level deep. Each Sub Directory will be cell value in a Worksheet .
    Example C:\Category\Year\Series\Month\Bimonthly\File Name.xls


    I will be using this in over 100 different files so I want to be Generic as possible

  • Re: Create Sub Directories From Cell Values


    see whether
    http://www.cpearson.com/excel/MakeDirMulti.htm
    is of any help.
    Use it maybe thus:


    Code
    mypathname = "C:\" & [A1] & "\" & [B1] & "\" & [C1] & "\" & [D1] & "\" & [E1]
    MakeDirMulti (mypathname)


    the names of folders were in cells A1 to E1


    p45cal

  • Re: Create Sub Directories From Cell Values


    Include the existing path and add the desierd sub directory


    examples:



    Code
    MkDir "C:\Category\Year"
    MkDir "C:\Category\Year\Series"
    MkDir "C:\Category\Year\Series\Month"
    MkDir "C:\Category\Year\Series\Month\Bimonthly"

    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Create Sub Directories From Cell Values


    Thanks for you quick reply. What I do not understand is if I put several files in the same final sub directory How does the macro determine if all the directory already exist if not make a second Path. Remember I want to use the same macro in several file to save different subdirectory. the only common element is the drive name.

  • Re: Create Sub Directories From Cell Values


    This code will test for the existance of a subdirectory specified in a cell on a worksheet. If it does not exist then it will be created.


    Code
    ' Assume a Directory string is in cell A1 such as
        '"C:\Category\Year" or "C:\Category\Year\Series"
        If Dir(Range("A1"), vbDirectory) = "" Then MkDir Range("A1")

    [hr]*[/hr] Auto Merged Post;[dl]*[/dl]Creates a new directory or folder.
    Syntax
    MkDir path
    The required path argument is a string expression that identifies the directory or folder to be created. The path may include the drive. If no drive is specified, MkDir creates the new directory or folder on the current drive.


    The MkDir VBA function will make a sub directory of the last named subdirectory in the path argument. It first attempts to find an existing path (less the named subdirectory) to be created. If this path does not exist the function will fail. The following procedure will check for each subdirectory in the path and, if not found, will create it. In other words it will build the path.


    [FONT="Arial Black"][COLOR="blue"][SIZE="4"]Bill[/SIZE][/COLOR][/FONT]
    Tip: To avoid chasing code always use Option Explicit.

  • Re: Create Sub Directories From Cell Values



    Excellent codes! Thank you very much, Bill Rockenbach.

  • Re: Create Sub Directories From Cell Values


    I use the vba listed here to create subdirectory and the one below for the file save.
    My question is what code do I need to add to change a .xlsm to .xlsx For Excel 2010.


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!