create folders with a file in it

snjpverma

Well-known Member
Joined
Oct 2, 2008
Messages
1,584
Office Version
  1. 365
Platform
  1. Windows
I have a list of names and the code number in column A1 to B10. sometimes, less than 10 names are also there.

e.g.:
John254689
James245682
Alisha458213

How can have a folder created with these names and codes.
Folders should look like this
John - 254689
James - 245682 ...

There is a file saved in a particular folder, this file should be copied and pasted in each of the new folder that gets created.
Please assist.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
The thing you need is a path in which you want to create the folders

VBA Code:
Sub jec()
   fpath = "C:\Users\xx\Downloads\"       'define your path here
   ar = Sheets(1).Cells(1, 1).CurrentRegion
   
   For i = 1 To UBound(ar)
     c00 = fpath & ar(i, 1) & "-" & ar(i, 2)
     If Dir(c00) = "" Then MkDir c00
   Next
End Sub
 
Upvote 0
I wanted to create the folders inside the folder named "Trial" which is on my Desktop, but instead the folders got created outside only i.e. on my desktop only. Secondly, all the folders' names got prefixed with the "Trial" as shown in the below screenshot.
Lastly, I want an excel file inside all the folders, but all the folders are blank. Thank You

1631828020803.png


The thing you need is a path in which you want to create the folders

VBA Code:
Sub jec()
   fpath = "C:\Users\xx\Downloads\"       'define your path here
   ar = Sheets(1).Cells(1, 1).CurrentRegion
 
   For i = 1 To UBound(ar)
     c00 = fpath & ar(i, 1) & "-" & ar(i, 2)
     If Dir(c00) = "" Then MkDir c00
   Next
End Sub
 
Last edited:
Upvote 0
blank reply got created by mistake. Mod please delete this blank reply.
 
Upvote 0
What is fpath? Is it on a network perhaps?
fpath = "C:\Users\Admin\Desktop\Trial"

it's on a Desktop, not on a network.

P.S.: As shown in the picture, the concerned folder named "Trial" is at the bottom, and the other folders are created outside it. Also, we can see how the folders are prefixed with the word Trial highlighted in green.

1631829549425.png
 
Last edited:
Upvote 0
It looks like a premission issue. Can you first try somewhere more accessible? Like, a folder on D: if you have, or C:\Test
 
Upvote 0
Oops, it was a silly mistake from my end.
fpath = "C:\Users\Admin\Desktop\Trial"
I wasn't using a \ at the end. so it should be "C:\Users\Admin\Desktop\Trial\"
After putting this, it worked.

Although the last problem remains. how do I insert a particular file in all the folders that got created?
 
Upvote 0
In the above code before for...next loop

VBA Code:
    Dim strFile As String
    strFile = "D:\Test VBA\000.xlsx"    ' modify this one
   
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    Dim oFile As Object
    Set oFile = fso.GetFile(strFile)

Inside the for...next loop right before "Next":

VBA Code:
    If not fso.FileExists(fso.BuildPath(c00, oFile.Name)) Then oFile.Copy fso.BuildPath(c00, oFile.Name)

Right before "End Sub"

VBA Code:
    Set oFile = Nothing
    Set fso = Nothing
 
Upvote 0
Solution
Will check this tomorrow and update you. Thanks for the help.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,210
Members
448,554
Latest member
Gleisner2

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