Problems Creating Folders With VBA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code...
Rich (BB code):
            strFile = distpath & mntxt
            If FileExists(strFile) = False Then
                MkDir strFile
            End If
            subpath2 = Format(Day(inq_date), "00") & " " & UCase(daytext)
            MkDir subpath1 & subpath2

mntxt = "August"
distpath = "D:/wsop 2020/Distributables/"
strFile = "D:/wsop 2020/Distributables/August"
subpath2 = "06 THU"

I use a function (FileExists) to check if this path exists, and if it doesn't, then create it. 'distpath' always exists, but if the month folder doesn't exist in it, then create the month (mntxt) folder.

Problem 1:
When 'August" exists in 'distpath', the FileExists function comes back = false and proceeds to create the folder "August" as if it didn't exist. The line in red results in a "Path/File access error." Looking for help in what is causing this error. Is it because the path exists already? If so, what would be causing the function to report the path being nonexistent when it truly exists. If the function returned true, it wouldn't try to recreate the folder.

Problem 2:
I am trying to add a new folder (subpath2) to the month folder. What I have in blue isn't the right approach. Can someone share how to create a new folder in an existing folder?
 
I did some more testing by writing a module that I could test different values of strFile for testing the FileExists function.
The function you posted is designed to find files not folders but you are trying to use it to find folders

Hopefully the 2 functions in posts #9 & #10 provide what you require
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hopefully the 2 functions in posts #9 & #10 provide what you require
It sure does!! When I went Googling in an effort to self educate, my resource didn't indicate that that method was specific for files (although it didn't indicate it did either). But now I, and likely others going forward, have a reliable answer.Thank you!!

But why did it work in my first two tests in post #8.

Thanks again Yongle.
 
Upvote 0
my resource didn't indicate that that method was specific for file
Therein lies the unseen big danger of relying on the almighty google ... whether researching VBA solutions ... or anything else whatsoever :eek:
CAVEAT GOOGLER!!

But why did it work in my first two tests in post #8.
Investigate as follows

Rich (BB code):
Amend your original function and BELOW this line:
TestStr = Dir(filePath)
Insert:
Debug.Print TestStr

Use those 2 tests values with the original function
The strings written to the immediate window may give you a clue
 
Upvote 0

Forum statistics

Threads
1,216,190
Messages
6,129,422
Members
449,509
Latest member
ajbooisen

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