Test if a folder exists, create if it doesn't?

  • Thread starter Thread starter 43fan
  • Start date Start date
4

43fan

I'm sure this is simple, but after going through the Excel help file, all it
has done is confuse me. ;) Basically, I just want to see if a folder
exists, if it does, change to the folder, if it doesn't, create it, then
change to there.

I'll be getting user inputs that will be concatenated to come up with the
folder name I'm looking for/changing to. As well, I'll use that same info
to name some files that will be saved into that folder.

Thanks!!
Shawn
PS - sorry for a double post, had to set my date back to run an update, and
forgot to change it to the correct day before I posted this before.



--
Join the newest in Fantasy Sports.

http://www.6FantasySports.com
(this site supports the Victory Junction Gang Camp with a portion of all
proceeds)
 
Hi

Try this:

Sub GoThere()
Dim NewFolder As String
NewFolder = "C:\Temp\NewOne"

On Error Resume Next
MkDir NewFolder
ChDrive NewFolder
ChDir NewFolder
'demo:
Application.Dialogs(xlDialogOpen).Show
End Sub
 
Hi

Use the Dir function
sub test
dim strMyFolder as string
strMyFolder = ""
strMyFolder = Dir("C:\MyfolderToCheck\")
if strMyFolder ="" then
msgbox "Folder does not exist
end if
end sub
Other solution, use the FileSystemObject.

Regards,
JY
 
This is one way

Dim FSO As Object
Dim oFolder As Object
Dim sFolder As String

sFolder = "C:\MyTest\abc"

Set FSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set oFolder = FSO.getfolder(sFolder)
On Error GoTo 0
If oFolder Is Nothing Then
MkDir sFolder
End If

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(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