Consulting

Results 1 to 9 of 9

Thread: Moving Folder to another Folder if name of folder and cell match

  1. #1

    Moving Folder to another Folder if name of folder and cell match

    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
    Last edited by SamT; 03-27-2018 at 07:19 AM. Reason: Inented Code

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    No 'End If's
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Quote Originally Posted by SamT View Post
    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.

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    IN what manner are the employee folders named?

    Give us an example(s) of the xVal(s) in an xCell
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Quote Originally Posted by SamT View Post
    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

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  7. #7
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Unless you must do his frequently, I would use Windows' Explorer. It is on the menu/Ribbon, Or
    Start >> Run + "explorer.exe"
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  8. #8
    Quote Originally Posted by SamT View Post
    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

  9. #9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •