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
716
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!
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Logit

Well-known Member
Joined
Aug 31, 2016
Messages
3,974
"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 ?
 

ER_Neha

Board Regular
Joined
Jul 25, 2020
Messages
130
Office Version
  1. 365
Platform
  1. Windows
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
 
Solution

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
716
Office Version
  1. 365
Platform
  1. Windows
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.
 

ER_Neha

Board Regular
Joined
Jul 25, 2020
Messages
130
Office Version
  1. 365
Platform
  1. Windows
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
 

willow1985

Well-known Member
Joined
Jul 24, 2019
Messages
716
Office Version
  1. 365
Platform
  1. Windows
Was not sure if it needed an "Else" or if it could just be deleted. Still learning this.

Thank you for re-sharing
 

Watch MrExcel Video

Forum statistics

Threads
1,127,998
Messages
5,628,046
Members
416,289
Latest member
Jbelisari

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