+ Reply to Thread
Results 1 to 9 of 9

VBA to save a workbook to a folder but create that folder if it doesn't already exist

  1. #1
    Registered User
    Join Date
    08-03-2012
    Location
    Wales
    MS-Off Ver
    2003
    Posts
    9

    VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Hi all, i have a room booking spreadsheet used by many users which is saved to their 'home' drives prior to being emailed to central hub. For good housekeeping reasons how can i get the code to create a folder on their home drives (eg Room Booking Forms) to save the .xls in if that folder doesn't exist. And to save in there if it does already exist?

  2. #2
    Forum Contributor
    Join Date
    07-26-2012
    Location
    USA
    MS-Off Ver
    Excel 2007 & 2010
    Posts
    351

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Please Login or Register  to view this content.
    Sorry, I just noticed you're looking for Home Drive, this will create on Desktop. I think you may be able to change Desktop to MyDocuments, but not 100% on that. If the folder doesn't exist, it will create it for you. If the folder does exist, it will not create it. Call the above method right BEFORE your save method. You'll need to declare DesktopFolderPath as a Public variable, and use it as your path in your save method you write.

    It's OK to say Thank You.
    Last edited by VBA FTW; 01-17-2014 at 11:59 AM.

  3. #3
    Registered User
    Join Date
    08-03-2012
    Location
    Wales
    MS-Off Ver
    2003
    Posts
    9

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Thanks VBA FTW.

    I've squeezed your lines, with some tweaks, into my command button but have come acrossa problem. When i click the button it creates the folder on the home drive with no concerns, but it does not save the .xls. Click again and it saves the .xls but in the root of the home drive not in the new folder!!
    where have i gone wrong?

    Please Login or Register  to view this content.
    many thanks

  4. #4
    Forum Contributor
    Join Date
    12-14-2013
    Location
    Tilburg, Nederland
    MS-Off Ver
    Excel 2010
    Posts
    256

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    The code code will try to make a directory, if it fails because directory exists then it wil safe the file....?

    It's may be simpler to check if directory exists and safe the file always.

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-03-2012
    Location
    Wales
    MS-Off Ver
    2003
    Posts
    9

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Thanks Gerard.

    Your code creates the folder and saves the file simultaneously but the file isn’t in the folder, it’s in the root of the home drive. Subsequent files saves error because the folder already exists (errors on the MkDir strFolderPath line).

  6. #6
    Forum Contributor
    Join Date
    12-14-2013
    Location
    Tilburg, Nederland
    MS-Off Ver
    Excel 2010
    Posts
    256

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    I see, didn't check for the backslashes.....

    Please Login or Register  to view this content.
    Edit: Folder check adjusted...

    Gerard
    Last edited by G e r a r d; 01-22-2014 at 06:22 AM. Reason: Error still existed.

  7. #7
    Registered User
    Join Date
    08-03-2012
    Location
    Wales
    MS-Off Ver
    2003
    Posts
    9

    Cool Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Gerard, many, many thanks

    one minor change, i removed the "\" from the & "Booking forms" and it worked a treat!!

    thank you, again

  8. #8
    Registered User
    Join Date
    03-06-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    45

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Sorry to hijack the thread but I have been looking every where for a solution and you guys seem to have the answers. VBA FTW, I am trying to use your code below. It works as written to create the folder on the desktop but, I need it to save the workbook with a file name listed in cell ("A51"). Any way you can help?

    HTML Code: 
    What I want is for my remote employees to click a command button which will check for the folder on the deskop. If it is not there create it and save the file.

    Thanks in advance,

  9. #9
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: VBA to save a workbook to a folder but create that folder if it doesn't already exist

    Try this...

    Please Login or Register  to view this content.
    Last edited by AlphaFrog; 02-24-2014 at 06:05 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Code to create New Folder (if doesn't exist) and then Save Workbook to folder
    By jenhawley in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-10-2013, 03:09 PM
  2. Replies: 13
    Last Post: 06-04-2012, 02:18 PM
  3. Save a file to a folder, create folder if it doesn't exist
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2010, 09:12 AM
  4. Replies: 6
    Last Post: 08-11-2006, 03:41 PM
  5. [SOLVED] Create Folder If It Doesn't Exist
    By Neutron1871 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-05-2005, 09:06 PM

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