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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel VBA Directory assistance 2

Status
Not open for further replies.

JAMES717

Technical User
Oct 13, 2009
15
US
I am trying to check to see if a directory exists. If it does not it needs to create the directory. If it does exists go to next procedure.

The code works if it does not exists, but does not work if the directory exists.

Code:

If Len(Dir("H:\Scorecards\")) = 0 Then
MkDir ("H:\Scorecards\")
End If
 
Set a reference to MicrosoftScriptingruntime and use filesystemobject:
Code:
Set fso = New FileSystemObject

If fso.FolderExists("String of folder path") = False Then
    'Create folder path
    fso.CreateFolder ("String of folder path") 
End If

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 


Hi,

Try this...
Code:
    Dim oFSO, oFolder
    
    On Error Resume Next
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFolder = oFSO.GetFolder(folderspec)
    
    If oFolder = Empty Then
        'makedir
    End If

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I am getting a User-defined type not defined on

Set fso = New FileSystemObject
 
xlbo said:
Set a reference to MicrosoftScriptingruntime

Rgds, Geoff

We could learn a lot from crayons. Some are sharp, some are pretty and some are dull. Some have weird names and all are different colours but they all live in the same box.

Please read FAQ222-2244 before you ask a question
 
To adapt your original so that it works, you should use:

Code:
[blue]If Len(Dir("H:\Scorecards\"[red],vbDirectory[/red])) = 0 Then
    MkDir ("H:\Scorecards\")
End If[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top