Create New Folder if Folder Doesn't Exist

ECUPIRATE

New Member
Joined
Jun 13, 2017
Messages
25
Hello! So I've created the following formula that should create a new folder based on the year, but if it already exists, I don't want to create the new folder. This formula seemed to work well, until I did a troubleshoot of "2018" by replacing TodayYear with "2018"... it did not work then. Can someone please help me identify the error in my code?

Code:
'.............Definitions...................................................................
FolderName = "" & Format(Now(), "yyyy mm dd")
FileName = Format(Now(), "yyyymmdd") & "_SpecialRequests" 
Path = "H:\DM\Requests"
TodayYear = Format(Now, "yyyy")
TodayFolder = Path & TodayYear & FolderName
'...........................................................................................


'Create new Folder in Requests Folder


If Len(Dir(Path & TodayYear, vbDirectory)) = 0 Then
Else
On Error Resume Next
MkDir (TodayFolder)
On Error GoTo 0
End If
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Code:
'.............Definitions...................................................................
FolderName = "" & Format(Now(), "yyyy mm dd")
Filename = Format(Now(), "yyyymmdd") & "_SpecialRequests"
Path = "H:\DM\Requests"
TodayYear = Format(Now, "yyyy")
TodayFolder = Path & TodayYear & FolderName
'...........................................................................................


'Create new Folder in Requests Folder
If Dir(TodayFolder) = Empty Then MkDir (TodayFolder)
 
Upvote 0
Code:
'.............Definitions...................................................................
FolderName = "" & Format(Now(), "yyyy mm dd")
Filename = Format(Now(), "yyyymmdd") & "_SpecialRequests"
Path = "H:\DM\Requests"
TodayYear = Format(Now, "yyyy")
TodayFolder = Path & TodayYear & FolderName
'...........................................................................................


'Create new Folder in Requests Folder
If Dir(TodayFolder) = Empty Then Thanks! But is there supposed to be an "otherwise" pMkDir (TodayFolder)

Thanks for your response! I tried this out but I get a run time error '76': Path not found. The path does exist though. I was able to save in a folder that already exists, just not create a new one. So it saved perfectly in the 2017 folder, but failed to create a 2018 folder.
 
Upvote 0
Okay, I was able to figure it out! First, I had too many definitions. So I eliminated FolderName & Updated TodayFolder by adding a "" between path and TodayYear. Thanks CalcSux78... your formula really helped point me in the right direction: I simply added On Error Resume Next to the formula, and voila!! Thanks again, you are appreciated. I was thinking too hard lol

Code:
'.............Definitions...................................................................[COLOR=#008000]'FolderName = "\" & Format(Now(), "yyyy mm dd") <---- Unnecessary[/COLOR]
FileName = Format(Now(), "yyyymmdd") & "_SpecialRequests"
Path = "[COLOR=#333333][I]"H:\DM\Requests"[/I][/COLOR]
TodayYear = "2017"
TodayFolder = Path [B]& "\" [/B]& TodayYear [COLOR=#008000]'& FolderName[/COLOR]
'...........................................................................................


'Create new Folder in Financial Request Letters Folder
[B]On Error Resume Next[/B]
If Dir(TodayFolder) = Empty Then MkDir (TodayFolder)

Thanks again!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,264
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top