Checking If A Folder Exists

Lotus Cars

Board Regular
Joined
Feb 4, 2011
Messages
55
Hi all,

I've got a bit of code to check if a folder is there before saving a file into it. Basically, I want to know if the period folder is there, and if it is, if a subfolder called Final P&L Reports is there. If either or both of the folders are not there, then create it.

It seems to work great if the folders are not there, but if either already exists, it just crashes out. Can anyone help please?

Cheers,

Lotus Cars


' Check folder is there, and if not, then create
If Period = 1 Then accountperiod = "P01"
If Period = 2 Then accountperiod = "P02"
If Period = 3 Then accountperiod = "P03"
If Period = 4 Then accountperiod = "P04"
If Period = 5 Then accountperiod = "P05"
If Period = 6 Then accountperiod = "P06"
If Period = 7 Then accountperiod = "P07"
If Period = 8 Then accountperiod = "P08"
If Period = 9 Then accountperiod = "P09"
If Period = 10 Then accountperiod = "P10"
If Period = 11 Then accountperiod = "P11"
If Period = 12 Then accountperiod = "P12"

If IsFileThere1("I:\Accts Cars\Cars Man Accounts 11-12\" & accountperiod) Then ActiveCell.Select
Else
MkDir "I:\Accts Cars\Cars Man Accounts 11-12\" & accountperiod
End If

If IsFileThere2("I:\Accts Cars\Cars Man Accounts 11-12\" & accountperiod & "\Final P&L & Reports") Then ActiveCell.Select
Else
MkDir "I:\Accts Cars\Cars Man Accounts 11-12\" & accountperiod & "\Final P&L & Reports\"
End If

Public Function IsFileThere1(FileName As String) As Boolean
If Len(Dir("I:\Accts Cars\Cars Man Accounts 11-12\" & accountperiod)) > 0 Then
IsFileThere = True
Else
IsFileThere = False
End If
End Function


Public Function IsFileThere2(FileName As String) As Boolean
If Len(Dir("I:\Accts Cars\Cars Man Accounts 11-12\" & accountperiod & "\Final P&L & Reports")) > 0 Then
IsFileThere = True
Else
IsFileThere = False
End If
End Function
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,224,550
Messages
6,179,462
Members
452,915
Latest member
hannnahheileen

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