VBA code to create a folder (if it doesn't already exist) based on cell values

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
886
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am hoping someone could help me write a certain code that can do the following:

If Cell D1 has text: "Update File Series"
Create a file in the directory specified in cell: Z1 and name it based on cell Y1.

If the file in this directory with this name does not exist, do not create and end sub.

So I suppose I want the code to not only run when Cell D1 has text: "Update File Series" but also check if the file already exists and then create it if it does not

I hope I am being clear. Thank you to anyone who can help with this!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
"If the file in this directory with this name does not exist, do not create and end sub."

I think you cleared this up in the next line of your post .... but ... don't you mean "If the file already exists, don't re-create the file and then end the sub ?
 
Upvote 0
Please try this
VBA Code:
Sub folder_exist_or_create()
Dim path As String
Dim folder As String
path = Range("Z1").Value & Range("Y1").Value
folder = Dir(path, vbDirectory)
If Range("D1").Value = "Update File Series" Then
    If folder = vbNullString Then
        VBA.FileSystem.MkDir (path)
        MsgBox "Folder created"
    Else
    MsgBox "Folder exist"
    End If
End If
End Sub
 
Upvote 0
Solution
Please try this
VBA Code:
Sub folder_exist_or_create()
Dim path As String
Dim folder As String
path = Range("Z1").Value & Range("Y1").Value
folder = Dir(path, vbDirectory)
If Range("D1").Value = "Update File Series" Then
    If folder = vbNullString Then
        VBA.FileSystem.MkDir (path)
        MsgBox "Folder created"
    Else
    MsgBox "Folder exist"
    End If
End If
End Sub

I do not want a message box as I want the folder created in the background. We have users who use the spreadsheet who may get flustered over these messages lol

and to answer logit's question: Yes; If the file already exists, don't re-create the file and then end the sub without any prompt.
 
Upvote 0
I suppose you could have deleted it by yourself anyway I am sharing the code again I hope it works for you
VBA Code:
Sub folder_exist_or_create()
Dim path As String
Dim folder As String
path = Range("Z1").Value & Range("Y1").Value
folder = Dir(path, vbDirectory)
If Range("D1").Value = "Update File Series" Then
    If folder = vbNullString Then
        VBA.FileSystem.MkDir (path)
    End If
End If
End Sub
 
Upvote 0
Was not sure if it needed an "Else" or if it could just be deleted. Still learning this.

Thank you for re-sharing
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,976
Members
448,934
Latest member
audette89

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