Create folders

feed99

New Member
Joined
Mar 12, 2009
Messages
4
Hi

Im trying to create folders based upon a list names inputted under one column (Column B for example). The names in the list are repeated and I dont want to create an individual folder for each entry. The list varies in length for each task and is therefore is not fixed.

I found one thread but only for specified ranges unfortunantly

Any ideas, VB not strong

thanks
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Welcome to the board!

Try this code for what you need, note that the code will create the new folders directly to the C drive, so for example if your first entry in column B was feed99 then your folder would be C:\feed99.

You will need to change this to the folder path where you want the new folders created, for example C:\Files\Test\feed99 (only edit the red text in the code).

Also note that the path specified (the red bit) must already exist for the folders to be created in or the code will fail.

Code:
Sub createfolder()
On Error GoTo Line1
    
For a = 2 To Cells(Rows.Count, "B").End(xlUp).Row
myfolder = "C:\" & Range("B" & a).Text
    If Not Dir(myfolder) = vbNullString Then
    GoTo Line1
    Else
     MkDir (myfolder)
    End If
Line1:
Next
End Sub

Hope this helps
 
Upvote 0
Thanks Jason,

Its creates the folders fine but there seems to be an issue with duplicated references. Ive looked at the code but it bugs out if a folder has already been created. I cant see why this happens. Any ideas?

Feed99

Welcome to the board!

Try this code for what you need, note that the code will create the new folders directly to the C drive, so for example if your first entry in column B was feed99 then your folder would be C:\feed99.

You will need to change this to the folder path where you want the new folders created, for example C:\Files\Test\feed99 (only edit the red text in the code).

Also note that the path specified (the red bit) must already exist for the folders to be created in or the code will fail.

Code:
Sub createfolder()
On Error GoTo Line1
 
For a = 2 To Cells(Rows.Count, "B").End(xlUp).Row
myfolder = "C:\" & Range("B" & a).Text
    If Not Dir(myfolder) = vbNullString Then
    GoTo Line1
    Else
     MkDir (myfolder)
    End If
Line1:
Next
End Sub

Hope this helps
 
Upvote 0
feed99

Why wouldn't it 'bug' out?

Have you tried manually creating >1 folder in the same directory?

If you do then I've a feeling Windows might throw up an error message.:)
 
Upvote 0
Oops, sorry feed, for some reason it worked for me and didn't trip, not sure why cos i had a duplicate name in the range i was using, but tried it now and tripped it with a few more entries in the list.

This code is doing the job on my new test sheet without tripping so hopefully will work for you as well.

Code:
Sub CreateFolder()
  For a = 2 To Cells(Rows.Count, "B").End(xlUp).Row
        myfolder = "C:\" & Range("B" & a).Text
    If Not CreateObject("Scripting.FileSystemObject").folderexists(myfolder) Then MkDir (myfolder)
  Next
End Sub

code sourced from Tek-Tips then changed to suit what you need
 
Upvote 0

Forum statistics

Threads
1,217,331
Messages
6,135,938
Members
449,973
Latest member
jarzack

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