+ Reply to Thread
Results 1 to 6 of 6

Creating Folders.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Creating Folders.

    Hello everyone. I am using the following VBA code to create a folder:

    Option Explicit
    
    Sub CreateFolder()
        Dim fso
        Dim fol As String
            fol = "L:\12_31_2009_157" ' change to match the folder path
    
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        If Not fso.FolderExists(fol) Then
        fso.CreateFolder (fol)
        Else
            MsgBox fol & " already exists!", vbExclamation, "Folder Exists"
        End If
        
    End Sub
    I am trying to create a code that is a little more "dynamic". I want to write VBA code that will create a userform where the user is prompted for the date (MM_DD_YYYY) for instance: "Please Enter the date". The macro will then take that date and create a folder named "MM_DD_YYYY_157. If the folder already exist I need an error message which will promt the user to enter in the correct date, "Incorrect date, please try again!"

    Any suggestions would be appreciated.
    Last edited by AnthonyWB; 04-08-2010 at 12:24 PM.

  2. #2
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Creating Folders.

    I also need to create two subloders in the "MM_DD_YYYY_157" folder named, "MM_DD_YYYY_157"_reports and "MM_DD_YYYY_157_Support_Summaries".

  3. #3
    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 Folders.

    Here's how I would write that:
    code removed...see below
    Last edited by JBeaucaire; 04-08-2010 at 11:53 AM.
    _________________
    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!)

  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 Folders.

    Correction:
    Option Explicit
    
    Sub CreateFolder()
    Dim fDate As String, fPath As String
    
    fDate = Application.InputBox("Enter a date in the format shown:", "Date to add...", Format(Date, "MM_DD_YYYY"))
    If fDate = "False" Then Exit Sub
    
    fPath = "L:\"
    
    On Error GoTo ErrorExit
        MkDir fPath & fDate & "_157"
        MkDir fPath & fDate & "_157\" & fDate & "_157_Reports"
        MkDir fPath & fDate & "_157\" & fDate & "_157_Support_Summaries"
        Exit Sub
        
    ErrorExit:
        MsgBox fPath & fDate & " already exists!", vbExclamation, "Folder Exists"
        
    End Sub

  5. #5
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Creating Folders.

    Thank you !

  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 Folders.

    You know, now that you're trying to make multiple folders, a little more robust response to finding folders already created is probably in order... try this and try running it on the same date more than once...
    Option Explicit
    
    Sub CreateFolder()
    Dim fDate As String, fPath As String
    Dim Fldr As String, ErrBuf As String
    
    fDate = Application.InputBox("Enter a date in the format shown:", "Date to add...", Format(Date, "MM_DD_YYYY"))
    If fDate = "False" Then Exit Sub
    
    fPath = "L:\"
    
    On Error GoTo ErrorHandler
        Fldr = fPath & fDate & "_157"
        MkDir Fldr
        Fldr = fPath & fDate & "_157\" & fDate & "_157_Reports"
        MkDir Fldr
        Fldr = fPath & fDate & "_157\" & fDate & "_157_Support_Summaries"
        MkDir Fldr
        If Len(ErrBuf) > 0 Then MsgBox "The following folders already existed:" & vbLf & vbLf & ErrBuf
        Exit Sub
        
    ErrorHandler:
        ErrBuf = ErrBuf & vbLf & Fldr
        Resume Next
        
    End Sub
    Last edited by JBeaucaire; 04-08-2010 at 01:29 PM.

+ 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