Macro that creates a directory but...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

How do I create a macro to look into a directory and create a new directory
called "Quotes" only if one doesn't exist already.

eg

R:\Team 318\Toyota

would result in...

R:\Team 318\Toyota\Quotes

when run again, it wouldn't do anything, because the Quotes directory now
exists.
 
Here is one I use to backup to the current directory. As you can see it
bypasses the mkdir if there is an error.

Sub Backup() 'kept in personal.xls & assigned to toolbar button
On Error GoTo BackupFile
MkDir CurDir & "\Backup"
BackupFile:
With ActiveWorkbook
MyWB = .Path & "\BACKUP\" & .Name
.SaveCopyAs MyWB
.Save
End With
End Sub
 
Use:

Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal
lpPath As String) As Long

MakeSureDirectoryPathExists "R:\Team 318\Toyota\Quotes\"

NOTE TRAILING \.
 
thanks...however get a compile error on the first line:

Compile error:

Only comments may appear after End Sub, End Function, or End Property
 
In your ThisWorkbook module, insert

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll"
(ByVal lpPath As String) As Long

Sub xx()
MakeSureDirectoryPathExists "c:\aal\aam\aan\"
End Sub

and then run xx: you'll get the idea.

PS: Private is not necessary in a code module.
 
This all goes on one physical line (although it might appear on several where
you are reading it) in the ThisWorkBookModule.

Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll"
(ByVal lpPath As String) As Long
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top