+ Reply to Thread
Results 1 to 10 of 10

Code to create New Folder (if doesn't exist) and then Save Workbook to folder

Hybrid View

  1. #1
    Registered User
    Join Date
    12-14-2012
    Location
    ohio
    MS-Off Ver
    Excel 2003
    Posts
    5

    Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Hello all,

    I am a newbie, so please be kind. I have been trying to correct this code for 2 days, and must admit I need help. I'm sure it's something simple, but can't crack it.

    I am using Excel 2007 and saving to a shared drive.

    I'm attempting to create a new folder, based on specific information in the workbook. If the folder already exists, I just need the workbook saved.
    The workbook also needs to be saved/named based on specific information in the workbook.

    The current code I have is:
    Sub SaveAs() Dim evnfol, newfol, sfile As String envfol = "\\MATTHEW-HP\Users\Matthew\Desktop\SCRATCH\Events\2013\" newfol = Format(CStr(Range("a13").Text), "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value sfile = Format(CStr(Range("a13").Text), "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value & Range("a11").Value If Len(Dir(envfol & newfol, vbDirectory)) = "" Then MkDir newfol End If ActiveWorkbook.SaveAs Filename:="envfol & newfol & " \ " & sfile", FileFormat:=52, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False End Sub
    Right now I'm receiving a error 13 type mismatch.
    Earlier today I was receving a error 1004 Method SaveAs of Workbook failed

    Thank you in advance for your assistance.
    Jenn
    Last edited by vlady; 01-07-2013 at 09:16 PM. Reason: code tags

  2. #2
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    I would at least change it to this:
    Sub SaveAs()
     
        Dim evnfol, newfol, sfile As String
        envfol = "\\MATTHEW-HP\Users\Matthew\Desktop\SCRATCH\Events\2013\"
        newfol = Format(CStr(Range("A13").Text), "mm.dd.yyyy") & Range("A9").Value & Range("B9").Value
        sfile = Format(CStr(Range("A13").Text), "mm.dd.yyyy") & Range("A9").Value & Range("B9").Value & Range("A11").Value
        If Len(Dir(envfol & newfol, vbDirectory)) = 0 Then      '<==changed
            MkDir envfol & newfol                               '<==changed
        End If
         ActiveWorkbook.SaveAs Filename:=envfol & newfol & "\" & sfile & ".xlsm", FileFormat:=52, Password:="", _ 
                   WriteResPassword:="", ReadOnlyRecommended:=False, CreateBackup:=False     '<==changed
    End Sub
    Last edited by rollis13; 01-09-2013 at 12:28 PM.

  3. #3
    Valued Forum Contributor rollis13's Avatar
    Join Date
    01-26-2012
    Location
    Cordenons
    MS-Off Ver
    Excel 2016 32bit - Win 11
    Posts
    935

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    ***cancelled***
    Last edited by rollis13; 01-07-2013 at 05:32 PM. Reason: double post

  4. #4
    Registered User
    Join Date
    12-14-2012
    Location
    ohio
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Thx Rolli13. I appreciate your reply!

    I had used that earlier today and was still receiving the error message. I will restore it.

    When you say "at least" change it, does that mean there are so many issues with the code, you don't know where to start?

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

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Try using filesystemobject instead of Dir:

    Option Explicit
    
    Sub CreateFileFolder()
    
      Dim evnfol$, newfol$, sfile$, fso As Object
    
      ' Get path of folder where the project will reside
        Set fso = CreateObject("Scripting.FileSystemObject")
        
        envfol = "\\MATTHEW-HP\Users\Matthew\Desktop\SCRATCH\Events\2013\"
        newfol = Format(CStr(Range("a13").Text), "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value
        sfile = Format(CStr(Range("a13").Text), "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value & Range("a11").Value  ' Alternate path
        
        fPath = envfol & newfol
        If Not fso.FolderExists(fPath) Then fso.CreateFolder (fPath)
        
        ActiveWorkbook.SaveAs Filename:="envfol & newfol & " \ " & sfile", FileFormat:=52
    
    End Sub
    Also I should note that your declarations are a bit off in that you have to declare each variable by itself or else Excel will consider it a variant. So in your example, evnfol and newfol are variants, while sfile is the only one being declared as a string. This syntax is different from other programming language where you can create group declarations.

    Hope this helps.

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

  6. #6
    Registered User
    Join Date
    12-14-2012
    Location
    ohio
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Good Morning Abousetta,

    Thank you again for your assistance. I applied the code and am still receiving the Error 13 Type Mismatch. It highlights the ActiveWorkbook.SaveAs line.

    Any other suggstions or changes?

    Thank you,
    Jenn

  7. #7
    Registered User
    Join Date
    12-14-2012
    Location
    ohio
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    I'm also receiving a error message which highlights:

    envfol = "\\MATTHEW-HP\Users\Matthew\Desktop\SCRATCH\Events\2013\"

    with the message "Compile error Variable not defined"


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

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Try this modified code:

    Option Explicit
    
    Sub CreateFileFolder()
    
      Dim envfol$, newfol$, sfile$, fPath$, fso As Object, wSh As Object, myDesktop$
      
      ' Get path of folder where the project will reside (on Desktop)
        Set wSh = CreateObject("Wscript.Shell")
        myDesktop = wSh.SpecialFolders("Desktop")
        envfol = myDesktop & "\SCRATCH\Events\2013\"
        newfol = Format(Range("a13").Text, "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value
        sfile = Format(Range("a13").Text, "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value & Range("a11").Value  ' Alternate path
        fPath = envfol & newfol & "\"
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(fPath) Then fso.CreateFolder (fPath)
        
      ' Save the workbook
        ActiveWorkbook.SaveAs Filename:=fPath & sfile & ".xlsm", FileFormat:=52
    
    End Sub
    It will fail if you don't have a folder called "SCRATCH\Events\2013\" residing on your desktop because it won't make several levels of folders but the code can be amended if needed.

    Let me know if this works for you.

    abousetta

  9. #9
    Registered User
    Join Date
    12-14-2012
    Location
    ohio
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Hello Abousetta,

    Well, we're still not having any luck/success with the code. I do have the following on my desktop: C:\Users\desktop\SCRATCH\Events\2013

    Now it's throwing the error code "Run Time Error 76 - Path not found" and highlighting the "Then fso.CreateFolder (fPath)" part of the code.

    Not quite sure what else to do.

    Thanks!Jenn

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

    Re: Code to create New Folder (if doesn't exist) and then Save Workbook to folder

    Hi Jenn,

    OK, let's test step by step...

    Option Explicit
    
    Sub CreateFileFolder()
    
      Dim envfol$, newfol$, sfile$, fPath$, fso As Object, wSh As Object, myDesktop$
      
      ' Get path of folder where the project will reside (on Desktop)
        envfol = "\\MATTHEW-HP\Users\Matthew\Desktop\SCRATCH\Events\2013\"
        newfol = Format(Range("a13").Text, "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value
        sfile = Format(Range("a13").Text, "mm.dd.yyyy") & Range("a9").Value & Range("b9").Value & Range("a11").Value  ' Alternate path
        fPath = envfol & newfol & "\"
        Msgbox fPath
        Set fso = CreateObject("Scripting.FileSystemObject")
        If Not fso.FolderExists(fPath) Then fso.CreateFolder (fPath)
        
      ' Save the workbook
        ActiveWorkbook.SaveAs Filename:=fPath & sfile & ".xlsm", FileFormat:=52
    
    End Sub
    Does this work? and is the path in the MessageBox correct?

    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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