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
929
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

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
"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,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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