VBA/Macro - Move forlder

acerlaptop

New Member
Joined
Feb 17, 2020
Messages
44
Office Version
  1. 2013
Platform
  1. Windows
Hi guys,

I have a code below that I'm currently working on. This code should move folder to another destination. But for some reason, the code moves the files inside the source folder, not the source folder itself.

VBA Code:
Sub MOVE_FOLDER()

Dim FSO As Object
Dim sFolder As String, dFolder As String

sFolder = "H:\TEST\New folder\" & ActiveSheet.Range("D2").value
dFolder = "H:\TEST\New folder\" & ActiveSheet.Name
Set FSO = CreateObject("Scripting.FileSystemObject")

If Left(Right(sFolder, 7), 4) = ActiveSheet.Name Then
If Not FSO.FolderExists(dFolder) Then
FSO.MoveFolder Source:=sFolder, Destination:=dFolder
MsgBox "Folder Moved Successfully to The Destination", vbExclamation, "Done!"
Else
MsgBox "Folder Already Exists in the Destination", vbExclamation, "Folder Already Exists!"
End If
End If

End Sub


Any thoughts please?

Thanks
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
This code should move folder to another destination. But for some reason, the code moves the files inside the source folder, not the source folder itself.
Hello!
You have the same path in the source and destination folder. Change sFolder or dFolder path string.
 
Upvote 0
Hello!
You have the same path in the source and destination folder. Change sFolder or dFolder path string.

I tried it with different path, still the same. Copies the files inside the folder, not the folder itself.
 
Upvote 0
I tried it with different path, still the same. Copies the files inside the folder, not the folder itself.
Could you write here:
- full paths with names of folders you need to move,
- value in D2 of active worksheet,
- active worksheet' name?
 
Upvote 0
I've made an example with some correction and comments of your code that works, and on which you can try different options.
VBA Code:
Option Explicit

Sub MOVEFOLDER()
Dim FSO As Object, sFolder$, dFolder$

'this point move and rename folder - from value of D2 cell to sheet's name:
'sFolder = "H:\TEST\" & ActiveSheet.Range("D2").Value
'dFolder = "H:\NEW_DIR\" & ActiveSheet.Name
'if the sFolder path (before &) and the dFolder path are the same, just rename folder

'and this move folder:
sFolder = "H:\TEST\" & ActiveSheet.Range("D2").Value
dFolder = "H:\NEW_DIR\"

Set FSO = CreateObject("Scripting.FileSystemObject")

'don't understand this string, verify if it correct
'If Left(Right(sFolder, 7), 4) = ActiveSheet.Name Then

    If FSO.folderExists(dFolder) = False Then
        FSO.MOVEFOLDER Source:=sFolder, Destination:=dFolder
        MsgBox "Moved successfully"
    Else
        MsgBox "Folder already exists"
    End If
    
'End If

End Sub
 
Upvote 0
I've made an example with some correction and comments of your code that works, and on which you can try different options.
VBA Code:
Option Explicit

Sub MOVEFOLDER()
Dim FSO As Object, sFolder$, dFolder$

'this point move and rename folder - from value of D2 cell to sheet's name:
'sFolder = "H:\TEST\" & ActiveSheet.Range("D2").Value
'dFolder = "H:\NEW_DIR\" & ActiveSheet.Name
'if the sFolder path (before &) and the dFolder path are the same, just rename folder

'and this move folder:
sFolder = "H:\TEST\" & ActiveSheet.Range("D2").Value
dFolder = "H:\NEW_DIR\"

Set FSO = CreateObject("Scripting.FileSystemObject")

'don't understand this string, verify if it correct
'If Left(Right(sFolder, 7), 4) = ActiveSheet.Name Then

    If FSO.folderExists(dFolder) = False Then
        FSO.MOVEFOLDER Source:=sFolder, Destination:=dFolder
        MsgBox "Moved successfully"
    Else
        MsgBox "Folder already exists"
    End If
 
'End If

End Sub

Hi,
I actually got my code to work (see below). But the thing is, it only moves the first folder that meets the criteria which is

VBA Code:
If Left(Right(sFolder, 7), 4) = ActiveSheet.Name

Can anybody get it to move all folder that meets the criteria, not just the first folder.


Here's the full code:

VBA Code:
Sub MOVE_FOLDER()

Dim fso As Object
Dim sFolder As String, dFolder As String

sFolder = "H:\TEST\New folder\" & ActiveSheet.Range("D2").Value
dFolder = "H:\TEST\New folder\" & ActiveSheet.Name & "\"
Set fso = CreateObject("Scripting.FileSystemObject")

If Left(Right(sFolder, 7), 4) = ActiveSheet.Name And fso.FolderExists(sFolder) = True Then
fso.MoveFolder Source:=sFolder, Destination:=dFolder
End If

End Sub
 
Upvote 0
Could you start your code in VBE by pressing F8 key, and then write here the result of ?ActiveSheet.Name in the Immediate window?
And I ask you again:
Could you write here:
- full paths with names of folders you need to move,
- value in D2 of active worksheet,
- active worksheet' name?
 
Upvote 0
Could you start your code in VBE by pressing F8 key, and then write here the result of ?ActiveSheet.Name in the Immediate window?
And I ask you again:


Source Folder = H:\TEST\New folder
Destination Folder = H:\TEST\New folder\2019\ (dFolder = "H:\TEST\New folder\" & ActiveSheet.Name & "\") -> ActiveSheet.Name = Year
Sub Folders to be moved (all in Source Folder) = 2019-Q1, 2019-Q2 and so on. (example only) - all subfolders with year in its name same to the activeworksheet.name should be moved.
Value in D2 = Subfolder Name

Thanks
 
Upvote 0
Ok. I'm trying to help, but you're ignoring my questions and aren't answering.
ActiveWorksheet.Name = "2019", for example. Good. Got it.
Now I need to understand the real value of D2 cell. Can you tell me what do you have as a result of ?[d2] in the Immediate window, when your code is running please?
 
Upvote 0

Forum statistics

Threads
1,215,317
Messages
6,124,232
Members
449,149
Latest member
mwdbActuary

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