Move complete folders into new created folders

drag1c

Board Regular
Joined
Aug 7, 2019
Messages
97
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Hi all,

I am using, similar to code below, code to copy/create new folders and files inside new folders. Does anyone know how I can edit this code, to move folders based on Range?

VBA Code:
Sub copyfiles()
Dim Cell As Range
Dim Foldername As String

Foldername = Range("F4").Text & " - " & Range("F3").Text

If Len(Dir("C:\Users\drag1c\Desktop\PROD\" & Foldername & "\", vbDirectory)) = 0 Then
        MkDir "C:\Users\drag1c\Desktop\PROD\" & Foldername & "\"
        MkDir "C:\Users\drag1c\Desktop\PROD\" & Foldername & "\" & "Pictures" & "\"
End If

Set fs = CreateObject("Scripting.FileSystemObject")
oldpath = "C:\Users\drag1c\Desktop\PROD\"
newpath = "C:\Users\drag1c\Desktop\PROD\" & Foldername & "\"
Set f = fs.GetFolder(oldpath)
Set NFile = f.Files
    For Each Cell In Worksheets("Sheet1").Range("E6:E10" & Cells(Rows.Count, 2).End(xlUp).Row)
        If Trim(Cell.Value) <> "" Then
            For Each pf1 In NFile
                NameFile = pf1.Name
                If InStr(NameFile, Cell.Value) > 0 Then
                  fs.MoveFolder Source:=oldpath, Destination:=newpath
                End If
            Next
        End If
    Next

End Sub

Part:
Code:
Set NFile = f.Files
I think here is problem how to get folder names, instead of file names.

Does anyone know how it should looks like?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Found a solution. Here below it is, if anyone ever needs it.

Key was in Set NFile = f.SubFolders

VBA Code:
Sub movefiles()
Dim Cell As Range
Dim Foldername As String

Foldername = Range("F4").Text & " - " & Range("F3").Text

If Len(Dir("C:\Users\drag1c\Desktop\PROD\" & Foldername & "\", vbDirectory)) = 0 Then
        MkDir "C:\Users\drag1c\Desktop\PROD\" & Foldername & "\"
        MkDir "C:\Users\drag1c\Desktop\PROD\" & Foldername & "\" & "Pictures" & "\"
End If

Set fs = CreateObject("Scripting.FileSystemObject")
oldpath = "C:\Users\drag1c\Desktop\PROD\"
newpath = "C:\Users\drag1c\Desktop\PROD\" & Foldername & "\"
Set f = fs.GetFolder(oldpath)
Set NFile = f.SubFolders
    For Each Cell In Worksheets("Sheet1").Range("E6:E10" & Cells(Rows.Count, 2).End(xlUp).Row)
        If Trim(Cell.Value) <> "" Then
            For Each pf1 In NFile
                NameFile = pf1.Name
                If InStr(NameFile, Cell.Value) > 0 Then
                  fs.MoveFolder oldpath & "\" & NameFile, newpath & "\" & NameFile
                End If
            Next
        End If
    Next

End Sub
 
Last edited:
  • Like
Reactions: yky
Upvote 0
Solution

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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