Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Alarmbloke
 
Posts: n/a
Default Help with Creating folders on Save

Hi,

Can any one help with this dilemma,

When clicking on a macro to save an excel file I need to create multiple
folders if they don't already exist, within the C directory,

IE C:\Folder1\Folder2\Folder3\Folder4\Folder5\filenam e.xls

Each Folder Name is allocated from a the contents of a Cell

IE Cell A1 = Folder 1
Cell A2 = Folder 2 etc

And the filename is also in a cell.

How do I get Excel to check if the folders exist and then create them if not
ready to save the final file in the new location??

Thanks

A puzzled excel student!!
  #2   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Help with Creating folders on Save

One way:

Option Explicit
Sub testme01()
Dim wks As Worksheet
Dim myCell As Range
Dim myPath As String
Dim testStr As String

Set wks = Worksheets("sheet1")

myPath = "C:"
With wks
On Error Resume Next
For Each myCell In .Range("a1:a5").Cells
If myCell.Value = "" Then
MsgBox "Ummmmmmm. Fill in those cells!"
Exit Sub
End If
myPath = myPath & "\" & myCell.Value
MkDir myPath
Next myCell
On Error GoTo 0
End With

testStr = ""
On Error Resume Next
testStr = Dir(myPath) & "\nul"
On Error GoTo 0

If testStr = "" Then
MsgBox "Error finding/creating: " & myPath
Exit Sub
End If

'do the work

End Sub

===
Or you could use an API function call:

Option Explicit
Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Sub testme02()
Dim myCell As Range
Dim myPath As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
For Each myCell In .Range("a1:a5").Cells
If myCell.Value = "" Then
MsgBox "Ummmmmmm. Fill in those cells!"
Exit Sub
End If
myPath = myPath & "\" & myCell.Value
MkDir myPath
Next myCell
End With

If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If
MakePath myPath
End Sub


Alarmbloke wrote:

Hi,

Can any one help with this dilemma,

When clicking on a macro to save an excel file I need to create multiple
folders if they don't already exist, within the C directory,

IE C:\Folder1\Folder2\Folder3\Folder4\Folder5\filenam e.xls

Each Folder Name is allocated from a the contents of a Cell

IE Cell A1 = Folder 1
Cell A2 = Folder 2 etc

And the filename is also in a cell.

How do I get Excel to check if the folders exist and then create them if not
ready to save the final file in the new location??

Thanks

A puzzled excel student!!


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.misc
Alarmbloke
 
Posts: n/a
Default Help with Creating folders on Save

Thanks again Dave.

Once again a perfect solution.

Must take a course on this Excel VBA lark, is there anything it wont do??
Wash the dishes, clean the car, keep the wife happy whilst I struggle away
with work???

"Dave Peterson" wrote:

One way:

Option Explicit
Sub testme01()
Dim wks As Worksheet
Dim myCell As Range
Dim myPath As String
Dim testStr As String

Set wks = Worksheets("sheet1")

myPath = "C:"
With wks
On Error Resume Next
For Each myCell In .Range("a1:a5").Cells
If myCell.Value = "" Then
MsgBox "Ummmmmmm. Fill in those cells!"
Exit Sub
End If
myPath = myPath & "\" & myCell.Value
MkDir myPath
Next myCell
On Error GoTo 0
End With

testStr = ""
On Error Resume Next
testStr = Dir(myPath) & "\nul"
On Error GoTo 0

If testStr = "" Then
MsgBox "Error finding/creating: " & myPath
Exit Sub
End If

'do the work

End Sub

===
Or you could use an API function call:

Option Explicit
Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Sub testme02()
Dim myCell As Range
Dim myPath As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
For Each myCell In .Range("a1:a5").Cells
If myCell.Value = "" Then
MsgBox "Ummmmmmm. Fill in those cells!"
Exit Sub
End If
myPath = myPath & "\" & myCell.Value
MkDir myPath
Next myCell
End With

If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If
MakePath myPath
End Sub


Alarmbloke wrote:

Hi,

Can any one help with this dilemma,

When clicking on a macro to save an excel file I need to create multiple
folders if they don't already exist, within the C directory,

IE C:\Folder1\Folder2\Folder3\Folder4\Folder5\filenam e.xls

Each Folder Name is allocated from a the contents of a Cell

IE Cell A1 = Folder 1
Cell A2 = Folder 2 etc

And the filename is also in a cell.

How do I get Excel to check if the folders exist and then create them if not
ready to save the final file in the new location??

Thanks

A puzzled excel student!!


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson
 
Posts: n/a
Default Help with Creating folders on Save

I've never seen it shovel snow--but that might not be a problem where you live!

Alarmbloke wrote:

Thanks again Dave.

Once again a perfect solution.

Must take a course on this Excel VBA lark, is there anything it wont do??
Wash the dishes, clean the car, keep the wife happy whilst I struggle away
with work???

"Dave Peterson" wrote:

One way:

Option Explicit
Sub testme01()
Dim wks As Worksheet
Dim myCell As Range
Dim myPath As String
Dim testStr As String

Set wks = Worksheets("sheet1")

myPath = "C:"
With wks
On Error Resume Next
For Each myCell In .Range("a1:a5").Cells
If myCell.Value = "" Then
MsgBox "Ummmmmmm. Fill in those cells!"
Exit Sub
End If
myPath = myPath & "\" & myCell.Value
MkDir myPath
Next myCell
On Error GoTo 0
End With

testStr = ""
On Error Resume Next
testStr = Dir(myPath) & "\nul"
On Error GoTo 0

If testStr = "" Then
MsgBox "Error finding/creating: " & myPath
Exit Sub
End If

'do the work

End Sub

===
Or you could use an API function call:

Option Explicit
Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Sub testme02()
Dim myCell As Range
Dim myPath As String
Dim wks As Worksheet

Set wks = Worksheets("Sheet1")

With wks
For Each myCell In .Range("a1:a5").Cells
If myCell.Value = "" Then
MsgBox "Ummmmmmm. Fill in those cells!"
Exit Sub
End If
myPath = myPath & "\" & myCell.Value
MkDir myPath
Next myCell
End With

If Right(myPath, 1) < "\" Then
myPath = myPath & "\"
End If
MakePath myPath
End Sub


Alarmbloke wrote:

Hi,

Can any one help with this dilemma,

When clicking on a macro to save an excel file I need to create multiple
folders if they don't already exist, within the C directory,

IE C:\Folder1\Folder2\Folder3\Folder4\Folder5\filenam e.xls

Each Folder Name is allocated from a the contents of a Cell

IE Cell A1 = Folder 1
Cell A2 = Folder 2 etc

And the filename is also in a cell.

How do I get Excel to check if the folders exist and then create them if not
ready to save the final file in the new location??

Thanks

A puzzled excel student!!


--

Dave Peterson


--

Dave Peterson
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
macro save a workbook whilst increasing file no shrek Excel Worksheet Functions 0 November 10th 05 02:40 PM
Creating folders and subfolders from excel file list afaubert Excel Discussion (Misc queries) 4 November 8th 05 11:44 PM
Creating a macro to save only partial data Jparsons Excel Discussion (Misc queries) 0 September 24th 05 10:50 AM
creating a macro for a "Save As" box MysticMarley Excel Discussion (Misc queries) 2 August 9th 05 02:31 AM
Excel creating temp files upon save. Joel Pratt Setting up and Configuration of Excel 2 May 18th 05 10:37 PM


All times are GMT +1. The time now is 10:11 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"