+ Reply to Thread
Results 1 to 7 of 7

Creating folderpaths based on variables

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Talking Creating folderpaths based on variables

    I'm looking to create folders on the fly with the folderpaths being based on defined variables.

    Could someone please have a look at my code below and tell me where Im going wrong?

    MainPath = "Z:\Mike\Retail Sept 2011\Macro\Tony Work\"
    
    '***Copy Data From DumpFile***
        Windows("Sample Data Dump.xls").Activate
            Selection.AutoFilter Field:=1, Criteria1:=ParentNSC
            Range("A2").Select
            Range(Selection, Selection.End(xlDown)).Select
            Range(Selection, Selection.End(xlToRight)).Select
            Selection.Copy
            
    '***Paste Date Into Template***
        Workbooks.Open Filename:=MainPath & "COINS Template.xls"
            Range("A2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                
    '***Define Region & Branch***
        Region = Range("B2")
        Branch = Range("C2")
                
        MkDir MainPath & "Outputs\" & "Region" & "\" & "Banch"
    Last edited by TonyforVBA; 10-24-2011 at 06:19 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Creating folderpaths based on variables

    I got frustrated long ago with making folders, so I wrote a routine to handle it for me in all my macros.
    Make Folders and Subfolders


    Use the UDF version of that function, put it in the same module, then you can call it like so:
    MakeFolders (MainPath & "Outputs\" & Region & "\" & Branch)
    It will make every folder and subfolder in that path if necessary.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Creating folderpaths based on variables

    To be truly "international I would use Application.PathSeparator instead of "\"
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Creating folderpaths based on variables

    Thanks, Roy, awesomely correct. I've updated to use that from now on.

  5. #5
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Re: Creating folderpaths based on variables

    Hi guys,

    Cheers for the feedback. However Im still not entirely sure on how to go about using this code.
    I do not have a list containg all the directories. Rather I wish to use cell referances set as variables to create folders within the directory.


    I'Ve included a snapshot of my code (including the given MakeDirectories Sub). Could you offer any advice?

    Sub MacroForBranchRetailLists()
    
    MainPath = "Z:\Mike\Retail Sept 2011\Macro\Tony Work"
    
    
    ' Open formatting
     ChDir _
            MainPath
        Workbooks.Open Filename:= _
            MainPath & "\Formatting Macro R2.xls"
    
    
    
    Dim filepaths(500) As Variant
    
        filepaths(1) = "931012"
        filepaths(2) = "931012"
        filepaths(3) = "935247"
        filepaths(4) = "934143"
        filepaths(5) = "931136"
        filepaths(6) = "931063"
        filepaths(7) = "934178"
        . . . . .
    
        . . . . . 
        filepaths(182) = "934305"
      
      'Loop
    For i = 1 To 182 'Or filepaths(i) = ""
        If Len(filepaths(i)) > 0 Then
                ProduceListings (filepaths(i))
           
        End If
    Next i
    
    
    End Sub
    
    
    Sub ProduceListings(ParentNSC)
    
    MainPath = "Z:\Mike\Retail Sept 2011\Macro\Tony Work\"
    
    '***Copy Data From DumpFile***
        Windows("Sample Data Dump.xls").Activate
            Selection.AutoFilter Field:=1, Criteria1:=ParentNSC
            Range("A1").Select
            Range(Selection, Selection.End(xlDown)).Select
            Range(Selection, Selection.End(xlToRight)).Select
            Selection.Copy
                
    '***Paste Date Into Template***
        Workbooks.Open Filename:=MainPath & "COINS Template.xls"
            Range("A2").Select
            Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
                
            Rows("2:2").Select
            Application.CutCopyMode = False
            Selection.Delete Shift:=xlUp
                
    '***Define Region & Branch***
        Region = Range("B2")
        Branch = Range("C2")
                
        Call MakeDirectories(Region, Branch)
                
    '***Save File***
        ActiveWorkbook.SaveAs Filename:="Z:\Tony\PCAR\Outputs\CGS Invest Data " & Owner & " " & file_name & ".xls", FileFormat:= _
            xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
            , CreateBackup:=False
            
            
    ' Run Formatting Macro
     '   Application.Run "'FormattingMacroPCAR.xls'!FormattingMacro"
    
    'ActiveWorkbook.Save
    ActiveWindow.Close
                
                
    
            
    
    End Sub
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    Sub MakeDirectories(Region, Branch)
    'Author:    Jerry Beaucaire
    'Date:      7/11/2010
    'Summary:   Create directories and subdirectories based
    '           on the text strings listed in column A
    '           Parses parent directories too, no need to list separately
    '           10/19/2010 - International compliant
    Dim Paths   As Range
    Dim Path    As Range
    Dim MyArr   As Variant
    Dim pNum    As Long
    Dim pBuf    As String
    Dim Delim   As String
    
    Set Paths = Range("A:A").SpecialCells(xlConstants)
    Delim = Application.PathSeparator
    On Error Resume Next
        
        For Each Path In Paths
            MyArr = Split(Path, Delim)
            pBuf = MyArr(LBound(MyArr)) & Delim
            For pNum = LBound(MyArr) + 1 To UBound(MyArr)
                pBuf = pBuf & MyArr(pNum) & Delim
                MkDir pBuf
            Next pNum
            pBuf = ""
        Next Path
    
    Set Paths = Nothing
    
    End Sub

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Creating folderpaths based on variables

    In post #2 I gave a "suggestion" on how you would call this macro based on your original macro.

    The MakeDirectories takes ONE parameter. It analyzes on long string and makes sure every folder exists, from the root directory all the way down to the final folder. The string should not include a file name, folders only.

    So, based on your original macro I made a guess at how to construct that one long path string and gave that example in post #2.

    So edit your macro to only pass one string. Your comma creates two string parameters, and only one is accepted. Give it another shot.

    BTW, do not edit the UDF itself the way you did, you made it unworkable. Go back to the original macro. The UDF version is further down on the sheet, you took the wrong version.
    Use the UDF version of that function, put it in the same module, then you can call it like so:
    MakeFolders (MainPath & "Outputs\" & Region & "\" & Branch)
    It will make every folder and subfolder in that path if necessary.
    Last edited by JBeaucaire; 10-20-2011 at 10:55 AM.

  7. #7
    Forum Contributor
    Join Date
    02-24-2010
    Location
    Dublin, Ireland
    MS-Off Ver
    Excel 2003
    Posts
    195

    Re: Creating folderpaths based on variables

    Thats worked perfectly for me. Thanks for your help. A very usefull piece of code.

+ 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