+ Reply to Thread
Results 1 to 5 of 5

Excel VB MkDir and File Save

  1. #1
    Registered User
    Join Date
    02-21-2004
    Posts
    9

    Smile Excel VB MkDir and File Save

    Hi

    I'm looking for code that will allow me to use the contents of a cell say (K1) on a spreadsheet to specify a directory\path where I want to save a file. I need a macro that will check if the directory\path exists, if not then create a directory based on the path detailed in the cell K1 eg G:\2009\Offers

    Then I want to save the file to that directory, with the file name being a concatenation of a series of cells within the sheet. This part I've got sorted.

    However, I can not work out the code to check if the directory exists and then make the directory.

    Note it is imperative that the directory path resides in a cell in the sheet as different users will have different drives/paths in which they will choose to save the file.

    Please help
    Steve

  2. #2
    Valued Forum Contributor
    Join Date
    06-16-2006
    Location
    Sydney, Australia
    MS-Off Ver
    2013 64bit
    Posts
    1,394

    Re: Excel VB MkDir and File Save

    Sounds like you could use a challenge and some coaching rather than a solution. Read up in the VBA help on "dir". Basically you call this function and it will return a code that you can trap to determine if the directory exists. You can then branch your code to create the directory if it is not there, then save the file. Be careful as you will need to check each level of the folder structure. Post your progress

  3. #3
    Forum Contributor
    Join Date
    04-16-2009
    Location
    Stellenbosch, South Africa
    MS-Off Ver
    Excel 2003; Excel 2007; Excel 2010; Excel 2013
    Posts
    136

    Re: Excel VB MkDir and File Save

    Hi Steve

    Have a look at GetSaveAsFilename in the help file (or show us your code so we can adapt). You could use this to collect a file path instead of having it keyed into a cell and then testing the keyed in value.

    You can still specify the file name (even as a concatenation of other cells). It basically used the same SaveAs dialog box which will allow a user to trawl their drives and create new folders. Then it will return the full path which you can then use when you save the file.

    HTH
    Regards

    Jon (Excel 2003, 2007, 2010, 2013)

  4. #4
    Registered User
    Join Date
    02-21-2004
    Posts
    9

    Exclamation Re: Excel VB MkDir and File Save

    Hi

    I have tried a couple of things but to be frank I'm self taught and usually keep trying until I get something that works.

    Here is the code I'm using, the rem'd lines were the last attempt I tried, I think I'm nowhere near a result!!

    Steve


    ' SaveOffer Macro
    ' Save Offer with Filename = "Model Variant Trans Material Code.xls" to Default Drive
    '
    ' Keyboard Shortcut: Ctrl+Shift+X
    '
    ' MyDir - InputBox("Enter Directory to Save , or OK to save to Directory", "MkDir", ActiveSheet.Range("K1"))
    '
    SavedName = InputBox("Enter Filename to Save , or OK to save as Worksheet", "Save Order", ActiveSheet.Range("A46"))
    If SavedName <> "" Then ActiveWorkbook.SaveAs Filename:=SavedName, FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

    End Sub

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983

    Re: Excel VB MkDir and File Save

    Please take a couple of minutes and read ALL theForum Rules then wrap your VBA code (Rule 3)
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

+ 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