To create folders using VBA

  • Thread starter Thread starter Sri
  • Start date Start date
S

Sri

Hello,

When I try to save an excel file using VBA and the folder
name mentioned in the filepath is not available, then
excel gives a error message..is there any way to force
excel to crate the folder and the save the file
accordingly..

I use FilesystemObject to create folders now...but just
wanted to know if there is any other way of doing the
same!!

Thanks,
Sri
 
Sub test()

If Not FolderExists("C:\Bob") Then
MkDir "C:\Bob"
End If

End Sub

'-----------------------------------------------------------------
Function FolderExists(Folder) As Boolean
'-----------------------------------------------------------------
Dim sFolder As String
On Error Resume Next
sFolder = Dir(Folder, vbDirectory)
If sFolder <> "" Then
If (GetAttr(sFolder) And vbDirectory) = vbDirectory Then
FolderExists = True
End If
End If
End Function


--

HTH

RP
(remove nothere from the email address if mailing direct)
 

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