Create Folders from Column Cells using FSO

Pinaceous

Well-known Member
Joined
Jun 11, 2014
Messages
1,124
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm working with
VBA Code:
FSO = CreateObject("Scripting.FileSystemObject")
where I have my code produce a Main Folder based off of its workbooks sheet cell value as its name.

Now, I would like to create a subfolders to put inside this Main Folder in using FSO.



So, far I have as follows:

VBA Code:
Sub CreateFolders()

Dim FSO As Object, sourceFolder As Object, file As Object

  Dim sPath As String, sMain As String, sFolder As String
  Dim i As Long

    Set FSO = CreateObject("Scripting.FileSystemObject")
    sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
    sMain = sPath & "\" & ThisWorkbook.Sheets(1).Range("B11").Value
      
  If Dir(sMain, vbDirectory) <> "" Then 'if the folder exists
  'Do nothing
    MsgBox "Duplicate Folder!"
    'MsgBox Application.UserName & "Duplicate Folder!"
    'MsgBox Environ("Temp") & " Hello!"
  
  Exit Sub
 
  Else ' 'if the folder not exists
      MkDir sMain
   
       FSO.MoveFile Source:="C:\Users\Desktop\" & Sheets(1).Range("E25").Value, Destination:=sMain & "\" & Sheets(1).Range("E25").Value

        'FSO.MoveFile Source:="C:\Users\Desktop\" & Sheets(1).Range("E31").Value, Destination:=sMain & "\" & Sheets(1).Range("E31").Value

        'FSO.MoveFile Source:="C:\Users\Desktop\" & Sheets(1).Range("E37").Value, Destination:=sMain & "\" & Sheets(1).Range("E37").Value

    End If
  
Set FSO = Nothing

 For i = 11 To 23
    sFolder = sMain & "\" & Sheets(1).Range("C" & i).Value
     
    If Dir(sFolder) = "" Then
      MkDir sFolder
    End If
  Next


End Sub



I'm getting an error with this line
VBA Code:
 FSO.CreateFolder "FSO.MoveFile Source:="C:\Users\Desktop\" & Sheets(1).Range("E25").Value, Destination:=sMain & "\" & Sheets(1).Range("E25").Value
.

Can someone help me?

Thanks!
pinaceous
 
Last edited:

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Hi pinaceous,

It looks like it should be...

VBA Code:
FSO.MoveFile Source:="C:\Users\Desktop\" & Sheets(1).Range("E25").Value, Destination:=sMain & "\" & Sheets(1).Range("E25").Value

Also, when that line is executed, it looks like the folder sMain & "\" & Sheets(1).Range("E25").Value does not exist.

Cheers!
 
Upvote 0
Hi Domenic!

Okay, I've corrected that line to
VBA Code:
FSO.Movefile "C:\Users\Desktop\" & Sheets(1).Range("E25").Value, sMain & "\" & Sheets(1).Range("E25").Value

But I'm getting an error probably because of what you already suggested:

Also, when that line is executed, it looks like the folder sMain & "\" & Sheets(1).Range("E25").Value does not exist.


Do you know how I can rewrite this line of the code to make it work??

Please let me know.

Many thanks!

Pinaceous
 
Upvote 0
Its giving me File not found (Error 53) even if I go to the length and provide:

VBA Code:
FSO.Movefile Source:="C:\Users\Desktop\myfile .xlsm" & ThisWorkbook.Sheets(1).Range("E25").Value, Destination:="C:\Users\Desktop\myfile .xlsm" & "\" & ThisWorkbook.Sheets(1).Range("E25").Value
 
Upvote 0
Hi Domenic!

Okay, I've corrected that line to
VBA Code:
FSO.Movefile "C:\Users\Desktop\" & Sheets(1).Range("E25").Value, sMain & "\" & Sheets(1).Range("E25").Value

But I'm getting an error probably because of what you already suggested:




Do you know how I can rewrite this line of the code to make it work??

Please let me know.

Many thanks!

Pinaceous

Try...

VBA Code:
'create the destination folder
MkDir sMain & "\" & Sheets(1).Range("E25").Value

'move file to newly created folder
FSO.Movefile "C:\Users\Desktop\" & Sheets(1).Range("E25").Value, sMain & "\" & Sheets(1).Range("E25").Value
 
Upvote 0
Try...

VBA Code:
'create the destination folder
MkDir sMain & "\" & Sheets(1).Range("E25").Value

'move file to newly created folder
FSO.Movefile "C:\Users\Desktop\" & Sheets(1).Range("E25").Value, sMain & "\" & Sheets(1).Range("E25").Value
Thanks Domenic! Good working with you again! Respectfully, pinaceous
 
Upvote 0

Forum statistics

Threads
1,223,716
Messages
6,174,069
Members
452,542
Latest member
Bricklin

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