VBA create a folder and add a file.

Pinaceous

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

I have a code to create a folder onto the desktop but I would like to know how I can insert a file, upon the execution of the code, automatically from the code into the newly created folder?

VBA Code:
Sub CreateFolders()

  Dim sPath As String, sMain As String, sFolder As String
  Dim i As Long
 
  sPath = CreateObject("WScript.Shell").SpecialFolders("Desktop")
  sMain = sPath & "\" & Sheets(1).Range("A1").Value
    
  If Dir(sMain) = "" Then
    MkDir sMain

'Add file Test.pdf from "C:\MISC\Test.pdf" 

  End If

 
End Sub

For example, if I have a file named "Test.pdf" into the folder creation, if I tell the code where the file is on my computer.

Please let me know, if you can help me.

Thank you,
pinaceous
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi
Your Feedback is highly appreciated and your support with (y)

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("A1").Value
   
  If Dir(sMain, vbDirectory) = "" Then MkDir sMain

    If Dir(sMain, vbDirectory) <> "" Then

    ' FSO.MoveFile Source:="C:\MISC\Test.pdf", Destination:=sMain & "\Test.pdf" ' copy and delete from source
    FSO.copyFile Source:="C:\MISC\Test.pdf", Destination:=sMain & "\Test.pdf" ' just copy

    End If
Set FSO = Nothing
End Sub
 
Upvote 0
Wow great code! That is what I was looking for. BTW, is there any way to improvise the code to auto-detect if the folder exists then not run the sub?
 
Upvote 0
Sorry do you mean like this
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("A1").Value
  
  If Dir(sMain, vbDirectory) <> "" Then 'if the folder exists
  'Do nothing
  Else ' 'if the folder not exists
      MkDir sMain
        FSO.copyFile Source:="C:\MISC\Test.pdf", Destination:=sMain & "\Test.pdf" ' just copy
    
    End If
Set FSO = Nothing
End Sub
 
Upvote 0
Hey Dossfm0q,

I'm coming back to this topic because I would like to know if it is possible to reference the name of the file listed within the FSO line of the code, within in workbook itself?

Something like, for example;

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

I tried this out but I'm getting a syntax error.

Please let me know.

Thank you,
pinaceous
 
Upvote 0
I think you need to remove the quote after the Range value
VBA Code:
FSO.MoveFile Source:="C:\Users\Desktop\" & Sheets(1).Range("A1").Value, Destination:=sMain & "\" & Sheets(1).Range("A1").Value
 
Upvote 0
Thanks Michael! I'll try it out and report back. Thanks again for your post. I appreciate your efforts. ✌
 
Upvote 0
Hey Michael,

The code works great but I noticed that it has trouble recognizing files that begin with an open prentices.

For example, if I use your code in Post#8 with a file that is named upon the sheet:

(2-STEP)2020 .xlsm

It will not pick it up as a file to be moved.

Would you have any idea on how I can go around this in using your code?

Many thanks,
pinaceous
 
Upvote 0

Forum statistics

Threads
1,214,899
Messages
6,122,155
Members
449,068
Latest member
shiz11713

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