MACRO - Check/Create Folder Structure And Save

MarkAn

Board Regular
Joined
Sep 28, 2005
Messages
69
Office Version
  1. 2010
Hi

I had previously posted something about this but I am still having problems, I am a novice with Macros.

I have an Excel spreadsheet that I need to save via a Macro, however, I need it to be saved in a specific location, which is compiled in a Cell in Excel I.e. D5 - returning the following location and file name::

C:\Documents and Settings\Administrator\My Documents\Downloads\Tick Sheet\2012 October\Sherman.xls

I believe that I need to use Dir and MKDir to first check if the location exists, if it does then Excel can save the file, if it doesn't then Excel would need to build the folder structure and then save the file.

Can someone please please help me, I would be very grateful.

Thanks
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
This will tell if there is an existing directory.

Code:
Sub IsIt()
MyPath = "C:\Documents and Settings\Administrator\My Documents\Downloads\Tick Sheet\2012 October"
If Dir(MyPath, vbDirectory) <> "" Then
MsgBox "Dir is there"
Else
MsgBox "Not there"
End If
End Sub
Code:
 
Upvote 0
This should do the trick:

Code:
Sub SaveFile()

    Dim sDest As String
    Dim sFolder As String
    
    sDest = ThisWorkbook.Worksheets("Sheet1").Cells(5, 4) 'Grab the value from cell D5
    
    sFolder = Left(sDest, InStrRev(sDest, "\")) 'Get the folder name
    
    CreateFolder (sFolder) 'Create the folder if it doesn't already exist.
    ThisWorkbook.SaveAs sDest 'Save the file in the folder


End Sub


'// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'// Procedure : CreateFolder | Sub
'// Author    : DarkSprout
'// Purpose   : Will Recursively Build ADirectory Tree
'// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sub CreateFolder(Folder)
On Error Resume Next
Dim objFSO As Object: Set objFSO = CreateObject("Scripting.FileSystemObject")
    If Folder <> "" Then
        If Not objFSO.FileExists(objFSO.GetParentFolderName(Folder)) Then
            Call CreateFolder(objFSO.GetParentFolderName(Folder))
        End If
        objFSO.CreateFolder (Folder)
    End If
End Sub
 
Upvote 0
Darren Bartrup

I would like to thank you fella, as this is exactly what I was looking for.

Many many thanks
 
Upvote 0

Forum statistics

Threads
1,214,866
Messages
6,121,996
Members
449,060
Latest member
mtsheetz

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top