Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create directories using Excel VBA 1

Status
Not open for further replies.

vestax22

Programmer
Jun 18, 2003
139
CA
Is it possible to verify if a directory exists using Excel VBA. If it doesn't I would like to create it in oreder to save a file inside it. Is this possible???


Thx
 
This should do what you want

ReportDirectory = "Name Of Folder"
If Dir(ReportDirectory, vbDirectory) = "" Then MkDir ReportDirectory

Rgds, Geoff
[blue]Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?[/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Hey Geoff

I would like to search a path for a certain directory. EX.

c:\Prod\ and the directory 2003. If the directory exists then use it and if not create it. Can you help?

Thx
 
That's exactly what I've given you
eg

ReportDirectory = "c:\Home\Test"
If Dir(ReportDirectory, vbDirectory) = "" Then MkDir ReportDirectory

If you run this and there is no folder called c:\home\test, it will make one. If there is one, it won't. What more do you want ?? This is not a helpdesk. You have to be able to figure these things out from examples that are given to you.


Rgds, Geoff
[blue]Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?[/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
sorry

I thought ReportDirectory was just a variable containing the name of the file and not the total path.

Thx for the help. I didn't want to tick you off.

Here's a star!
 
Hey Geof, one more question. How do I do the same thing except with a dile instead of a dir??

Thx
 
Another way:

Code:
Sub Create_Folder_If_NonExistant()
Dim f, fso
On Error GoTo FileExists
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateFolder("C:\My Documents\Test")
Set fso = Nothing
Set f = Nothing
Exit Sub

FileExists:
MsgBox "This folder already exists!!!", vbCritical, "Folder Exists"
Set fso = Nothing
Set f = Nothing
End Sub

Have a beer Geoff! [cheers] TGTIF!!!!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
For a file - set the same ref to scripting runtime:


dim fExists as boolean
Set fs = CreateObject("Scripting.FileSystemObject")
fExists = fs.FileExists("C:\home\FileName.xls")

fExists will be TRUE if the file exists and FALSE if not

Rgds, Geoff
[blue]Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?[/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Hi Geoff,

If vestax22 had asked about Files, then I would have posted that too!!! ;-) [thumbsup2]

Cheers!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
He did.....well "diles" anyway
probably posted at the same time as you by the looks of it ;-)

Rgds, Geoff
[blue]Quantum materiae materietur marmota monax si marmota monax materiam possit materiari?[/blue]
Want the [red]best[/red] answers to your questions ? faq222-2244
 
Oooops, [blush]

Sorry Geoff!

Shoot Me! [Cannon]

Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top