VBA to create sub folder when it does not exist

gusbus

New Member
Joined
Mar 31, 2022
Messages
9
Office Version
  1. 2021
Platform
  1. Windows
So I have adapted this VBA code for my purposes but need help making an additional change. When the sub folder does not exist I need to have the program create the sub folder and run the file moving portion again.

Thanks in advance.


VBA Code:
Sub MyMoveFiles()


    Dim myDestDir As String
    Dim myFileExt As String
    Dim i As Long
    Dim myFilePrefix As String
    Dim myFile
    Dim mySrc As String
    Dim mySubDest As String
    Dim myDest As String
    Dim DelFile As Boolean


'   Set up an array for all the different directories you wish to copy files from
'   Number in parentheses of variable declaration should be number of items in array - 1
    Dim mySourceDir(1)
    mySourceDir(0) = "C:\Users\Documents\FilesToBeMoved0\"
    mySourceDir(1) = "C:\Users\Documents\FilesToBeMoved1\"
    
'   Set source directory where subfolders are found
    myDestDir = "C:\Users\Documents\EntityFilings\"
    
'   Designate file extensions to move
    myFileExt = "*.*"
    
    
'   ***********************************************************************************
    
'   Loop through all each directory
    For i = LBound(mySourceDir) To UBound(mySourceDir)
'       Loop through each Excel file in each directory
        myFile = Dir(mySourceDir(i) & myFileExt)
        Do While Len(myFile) > 0
'           Get file prefix
            myFilePrefix = Left(myFile, 4)
'           Build source and destination references
            mySrc = mySourceDir(i) & myFile
'           Specify the sub folder
            mySubDest = Sheet4.Range("B4").Value
            myDest = myDestDir & myFilePrefix & "\" & mySubDest & "\" & myFile
'           Set boolean value to delete file
            DelFile = True
'           Copy file from source to destination
            On Error GoTo No_Folder
            FileCopy mySrc, myDest
            On Error GoTo 0
'           Delete source file, if flag is true
            If DelFile = True Then Kill mySrc
'           Reinitialize myFile
            myFile = Dir
        Loop
    Next i
    
    MsgBox "Moves complete!"
    
    Exit Sub
    
No_Folder:
'   If cannot find direcory for a file, do not delete, return message box, and continue
    If Err.Number = 76 Then
        DelFile = False
        MsgBox "Folder " & myDestDir & myFilePrefix & "\" & mySubDest & " does not exist.", vbOKOnly, _
               "Cannot move file " & mySrc & "!!!"
        Err.Clear
        Resume Next
    Else
        MsgBox Err.Number & ": " & Err.Description
    End If


End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
just run MakeDir() every time.
if its not there, it makes it,
if it is there, no harm done, it moves on.

Code:
Public Sub MakeDir(ByVal pvDir)
Dim fso
On Error GoTo errMake
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(pvDir) Then fso.CreateFolder pvDir     'MkDir pvDir
Set fso = Nothing
Exit Sub
errMake:
'MsgBox Err.Description & vbCrLf & pvDir, , "MakeDir(): " & Err
Set fso = Nothing
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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