[VBA] Create Folder MkDir

Solved
Palteza Posted messages 82 Status Member -  
Palteza Posted messages 82 Status Member -
Hello, how are you doing?

I'm creating a folder using the MkDir method if it doesn't exist. The minor problem is... that it doesn't get created ^^

Here's the portion of the code:

SourcePath = Left(Workbooks("ExtractionTest.xls").FullName, InStr(Workbooks("ExtractionTest.xls").FullName, "ExtractionTest.xls") - 1) Year = 2006 If Dir(SourcePath & Year & "\Données_Découpages", 16) = "" Then MkDir (SourcePath & Year & "\Données_Découpages")

I'm getting something like this with SourcePath: C\Documents....\..\..\ and everything should follow, but it doesn't.

The thing is that when I remove Year, it works perfectly. I've tried both Year as Integer and String, but that's not it.

It's giving me a runtime error 76, path not found on:
MkDir (SourcePath & Year & "\Données_Découpages")


Thanks in advance.

6 answers

Bidouilleu_R Posted messages 1209 Status Member 296
 
your code is correct except for this
you don't check if the 'Year' folder already exists...

you are not allowed to create a subfolder for a nonexistent folder.
it must be done in two steps: folder then subfolder
Sub test() CheminSource = Left(Workbooks("ExtractionTest.xls").FullName, InStr(Workbooks("ExtractionTest.xls").FullName, "ExtractionTest.xls") - 1) Annee = 2006 If Dir(CheminSource & Annee, 16) = "" Then MkDir (CheminSource & Annee) End If If Dir(CheminSource & Annee & "\Données_Découpages", 16) = "" Then MkDir (CheminSource & Annee & "\" & "Données_Découpages") End If End Sub
5