Problems Creating Folders With VBA

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,889
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?
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
3,889
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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.
 

Yongle

Well-known Member
Joined
Mar 11, 2015
Messages
6,977
Office Version
  1. 365
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,130,219
Messages
5,640,955
Members
417,182
Latest member
mgcorreia

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
Top