PDA

View Full Version : Moving Folder to another Folder if name of folder and cell match



aquse
03-26-2018, 07:38 AM
Hello everyone,


I basically need to move a folder containing to another folder if cell and name folder match. I got this code from the internet but it only moves files and the extension of the file needs to be included as well. However it does not move folders.


Sub movefiles()'Updateby Extendoffice
Dim xRg As Range, xCell As Range
Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
Dim xSPathStr As Variant, xDPathStr As Variant
Dim xVal As String

On Error Resume Next
Set xRg = Application.InputBox("Please select the file names:", "KuTools For Excel", ActiveWindow.RangeSelection.Address, , , , , 8)

If xRg Is Nothing Then Exit Sub
Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
xSFileDlg.Title = " Please select the original folder:"
If xSFileDlg.Show <> -1 Then Exit Sub
xSPathStr = xSFileDlg.SelectedItems.Item(1) & "\"
Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
xDFileDlg.Title = " Please select the destination folder:"
If xDFileDlg.Show <> -1 Then Exit Sub

xDPathStr = xDFileDlg.SelectedItems.Item(1) & "\"

For Each xCell In xRg
xVal = xCell.Value
If TypeName(xVal) = "String" And xVal <> "" Then
FileCopy xSPathStr & xVal, xDPathStr & xVal
Kill xSPathStr & xVal
End If
Next
End Sub

How can i amend this code for it to move folders.

Thank you,

Aquse

SamT
03-27-2018, 07:20 AM
No 'End If's

aquse
03-27-2018, 07:33 AM
No 'End If's


Please explain. I have no programming experience what so ever, sorry.

Basically i work an HR department and have a list of about 2k employees in an excel file and need to move their folders to the archive folder.

SamT
03-28-2018, 07:11 AM
IN what manner are the employee folders named?

Give us an example(s) of the xVal(s) in an xCell

aquse
03-28-2018, 08:39 AM
IN what manner are the employee folders named?

Give us an example(s) of the xVal(s) in an xCell

So a folder is named "John MacDonald" and the cell name is the same "John MacDonald". It does not move. Same with files, lets say a pdf file is called "X" and the cell text is also "X", it won't move. It will only work if the cell name is "X.pdf". I tried to google the extension for the folders but i do not think there is one because it's a directory.

Thank you,

Aquse

SamT
03-28-2018, 09:39 AM
As for moving the folders, Unless this is a continuing problem, I would just use Windows' Explorerfor a one time deal.

Start Menu >> Run + "explorer.exe"

"Explorer.exe" is like a giant "Folder Picker"+"File Picker"+many actions, (drag&drop, Cut, Copy, Paste, Move, Rename, etc)

Note that after any Action, Right clicking, in the right hand pane of the original folder, will let you "Undo... " any action.

SamT
03-28-2018, 09:42 AM
Unless you must do his frequently, I would use Windows' Explorer. It is on the menu/Ribbon, Or
Start >> Run + "explorer.exe"

aquse
03-29-2018, 04:33 AM
Unless you must do his frequently, I would use Windows' Explorer. It is on the menu/Ribbon, Or
Start >> Run + "explorer.exe"

The problem is, i have a folder named "employees" which has about 5k employees, and received a list of leavers (450) in an excelfile. While the explorer.exe helps, it will still take a very long time since the leavers are all scattered in the employee folder, and one employee(folder) takes about 5 seconds to move since the computer is old. My thinking was to run the program and leave it for two or 3 hours hoping it won't crash(or do it batches). Plus employees leave every month and in a month i will receive the same list, just with different names.

Thank you,

Aquse

aquse
04-18-2018, 02:44 AM
bump