Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Create Folders From Excel

I have some code that saves several copies of a spreadsheet to a specific
folder and it works great. However, if the folder that is specified in the
code doesn't exist, it errors out. Is there a way to have the folders be
created via VB code to circumvent this? I have to create folders on a daily
basis, and this would save some annoyance.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Create Folders From Excel

You can play with this

Sub MakeDirectory()
Dim Dirname As String
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dirname = "C:\MyDir"
If Not fs.FolderExists(Dirname) Then
fs.CreateFolder Dirname
Else
' do nothing
End If
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"WBTKbeezy" wrote in message ...
I have some code that saves several copies of a spreadsheet to a specific
folder and it works great. However, if the folder that is specified in the
code doesn't exist, it errors out. Is there a way to have the folders be
created via VB code to circumvent this? I have to create folders on a daily
basis, and this would save some annoyance.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default Create Folders From Excel

Thanks Ron, simple and effective!

"Ron de Bruin" wrote:

You can play with this

Sub MakeDirectory()
Dim Dirname As String
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
Dirname = "C:\MyDir"
If Not fs.FolderExists(Dirname) Then
fs.CreateFolder Dirname
Else
' do nothing
End If
End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm


"WBTKbeezy" wrote in message ...
I have some code that saves several copies of a spreadsheet to a specific
folder and it works great. However, if the folder that is specified in the
code doesn't exist, it errors out. Is there a way to have the folders be
created via VB code to circumvent this? I have to create folders on a daily
basis, and this would save some annoyance.


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Create Folders From Excel

Dim mpDir As String
Dim i As Long
Dim mpDirParts As Variant

mpDir = "C:\test\subdir\subsubdir\bottom"
mpDirParts = Split(mpDir, "\")
mpDir = mpDirParts(LBound(mpDirParts))
For i = LBound(mpDirParts) + 1 To UBound(mpDirParts)

mpDir = mpDir & "\" & mpDirParts(i)
On Error Resume Next
MkDir mpDir
On Error GoTo 0
Next i


--
---
HTH

Bob

__________________________________________
UK Cambridge XL Users Conference 29-30 Nov
http://www.exceluserconference.com/UKEUC.html

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"WBTKbeezy" wrote in message
...
I have some code that saves several copies of a spreadsheet to a specific
folder and it works great. However, if the folder that is specified in the
code doesn't exist, it errors out. Is there a way to have the folders be
created via VB code to circumvent this? I have to create folders on a
daily
basis, and this would save some annoyance.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Create Folders From Excel

The following code will create the specified folder(s) as required starting
from the root down to the deepest nested folder. You must pass it a fully
qualified path specification (including drive letter).

Sub MakeMultiDir(PathSpec As String)
Dim Arr As Variant
Dim N As Long
Dim S As String
If InStr(1, PathSpec, ":", vbBinaryCompare) = 0 Then
MsgBox "You must use a fully qualified path."
Exit Sub
End If
Arr = Split(PathSpec, "\")
For N = LBound(Arr) To UBound(Arr)
S = S & Arr(N) & "\"
On Error Resume Next
MkDir S
Next N
End Sub

For example,

MakeMultiDir "C:\Test1\Test2\Test3\Test4"

will create
C:\Test1
C:\Test1\Test2
C:\Test1\Test2\Test3
C:\Test1\Test2\Test3\Test4

It does nothing if a folder already exists.

This is a simplification of the code at
http://www.cpearson.com/Excel/MakeDirMulti.htm.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"WBTKbeezy" wrote in message
...
I have some code that saves several copies of a spreadsheet to a specific
folder and it works great. However, if the folder that is specified in the
code doesn't exist, it errors out. Is there a way to have the folders be
created via VB code to circumvent this? I have to create folders on a
daily
basis, and this would save some annoyance.




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
Can I create folders in explorer from an excel list njmcr Excel Discussion (Misc queries) 5 February 28th 13 12:22 PM
Can excel automatically create folders Can excel automatically create folders Excel Discussion (Misc queries) 1 June 23rd 08 09:38 PM
Create list of folders kaiser Excel Programming 1 July 20th 07 07:50 PM
Can anyone help me Create Excel list of files in windows folders solrac1956 Excel Worksheet Functions 1 November 28th 05 11:07 PM
To create folders using VBA Sri Excel Discussion (Misc queries) 1 February 4th 05 01:13 PM


All times are GMT +1. The time now is 10:29 AM.

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

About Us

"It's about Microsoft Excel"