Closed Thread
Results 1 to 11 of 11

Copy and Paste Folder With VBA

Hybrid View

  1. #1
    Registered User
    Join Date
    07-06-2007
    Posts
    70

    Copy and Paste Folder With VBA

    Hi all,

    First off - i have zero knowledge using vba within excel with basic knowledge on VB script itself.

    I want to know if it would be possible to copy and paste a folder using vba which would then be renamed using a value from the worksheet.

    as i say - i have no idea if this is even possible.

    I look forward to your advice, if any.

    Thanks,
    cj
    Last edited by VBA Noob; 07-07-2008 at 11:57 AM.

  2. #2
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    macro

    HI
    it is possible. Need more details to give you specific codes
    Ravi

  3. #3
    Registered User
    Join Date
    07-06-2007
    Posts
    70
    Thanks for the reply

    Basically i have a folder of files which are a sort of template. i want to be able to copy the whole folder and rename the folder.

    example.

    if the folder was c:/myFiles/template/

    i want to copy and paste the folder template and rename the folder (again, if possible) using the value in cell A1 (which would probably have a name serial for example).

    i would end up with
    c:/myFiles/templateRenamed/

    i hope that makes sense, if not please just let me know.

    Thanks in advance!
    cj

  4. #4
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573
    Moving a folder is the same as renaming it. No copy is needed.

    Here are 2 routines to play with. Be sure to backup your folders before attempting this. Some of these require a trailing "\" and some do not.

    Be sure to first add the reference in Tools > Reference, since they use the early binding method.
    Sub MoveFolder()
    Rem Needs Reference: MicroSoft Script Runtime, scrrun.dll
    Rem Instructions: http://support.microsoft.com/default.aspx?scid=kb;en-us;186118
        Dim FSO As Scripting.FileSystemObject
        Dim sSource As String
        Dim sDest As String
         
        Set FSO = New Scripting.FileSystemObject
         
        sSource = Range("A1").Value
        sDest = Range("A2").Value
        FSO.MoveFolder sSource, sDest
    End Sub
    
    Sub CopyFolder()
    Rem Needs Reference: MicroSoft Script Runtime, scrrun.dll
    Rem Instructions: http://support.microsoft.com/default.aspx?scid=kb;en-us;186118
        Dim FSO As Scripting.FileSystemObject
        Dim sSource As String
        Dim sDest As String
         
        Set FSO = New Scripting.FileSystemObject
         
        sSource = Range("A1").Value
        sDest = Range("A2").Value
         
        FSO.CopyFolder sSource, sDest, True
    End Sub

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello cjconner24,

    I wasn't sure exactly what you wanted to do, so I created a flexible macro that should do what you need. The macro uses late binding so you don't need to add a library reference to your project. This also allows the code to run directly on other machines. This macro allows you to rename a folder, or copy a folder to a new location, and lets you choose if the files in the folder will be overwritten when the folder is copied. The macro assumes the existence of a destination folder. It will not create one for you.

    This macro has 3 arguments. The first argument is required and is the source file path. The second argument, which is optional, is the destination folder path. The source will be copied to the destination and renamed using the text of cell "A1". The third argument, which is optional, determines if the folder will overwrite th files in it. The default is False. You can change the cell that contains the new folder name by changing the variable NewName in the code. All error checking and handling is done in the macro.
    Sub CopyRenameFolder(SrcFolderPath As String, Optional DstFolderPath As String, Optional OverWriteFiles As Boolean)
    
      Dim DstFolder As Object
      Dim FolderPath As String
      Dim FSO As Object
      Dim NewName As String
      Dim SrcFolder As Object
      
        'Initialize variables and objects.
         NewName = Range("A1").Text
         Set FSO = CreateObject("Scripting.FileSystemObject")
         
        'Check the file path string is properly formed.
         If DstFolderPath = "" Then DstFolderPath = SrcFolderPath
         If Right(SrcFolderPath, 1) <> "\" Then SrcFolderPath = SrcFolderPath & "\"
         If Right(DstFolderPath, 1) <> "\" Then DstFolderPath = DstFolderPath & "\"
        
        'Assign the source and destination folders.
         On Error GoTo CopyError
           FolderPath = SrcFolderPath
             Set SrcFolder = FSO.GetFolder(SrcFolderPath)
           FolderPath = DstFolderPath
             Set DstFolder = FSO.GetFolder(DstFolderPath)
         On Error GoTo 0
        
        'Verify the new name exists.
         If NewName = "" Then
           MsgBox "Destination Folder missing New Name.", _
             vbOKOnly + vbCritical, "Folder Not Renamed"
           GoTo Finished
          End If
        
         'Copy source folder to the destination folder if they are different
          If SrcFolderPath <> DstFolderPath Then
            SrcFolder.Copy DstFolderPath, OverWriteFiles
          End If
         
         'Rename the destination folder using the new name.
          On Error Resume Next
            DstFolder.Name = NewName
             'Ignore error if old name is same as new name.
              If Err = 58 Then GoTo Finished
         
    CopyError:
            If Err <> 0 Then
              MsgBox Err.Description & vbCrLf & FolderPath, _
              vbOKOnly + vbCritical, "Folder Not Copied"
            End If
        
    Finished:
       'Free object from memory
        Set FSO = Nothing
          
    End Sub
    Adding the Macro
    1. Copy the macro above pressing the keys CTRL+C
    2. Open your workbook
    3. Press the keys ALT+F11 to open the Visual Basic Editor
    4. Press the keys ALT+I to activate the Insert menu
    5. Press M to insert a Standard Module
    6. Paste the code by pressing the keys CTRL+V
    7. Make any custom changes to the macro if needed at this time
    8. Save the Macro by pressing the keys CTRL+S
    9. Press the keys ALT+Q to exit the Editor, and return to Excel.

    Examples
      'Rename the folder
        $A$1 = "TemplateRenamed"
        CopyRenameFolder "C:\myFiles\template\"
    
      'Copy folder to new location and rename it using $A$1
        CopyRenameFolder "C:\myFiles\template\", "C:\Temp\"
    
        Copies the folder "Template" to the folder "Temp" and then renames the folder "TemplateRenamed". The new folder path would be   "C:\Temp\TemplateRenamed".
    
      'Copy folder to new location, rename it, and overwrite files
        CopyRenameFolder "C:\myFiles\template\", "C:\Temp\", True

  6. #6
    Registered User
    Join Date
    07-06-2007
    Posts
    70
    Thanks for all the great help. This is exactly what i was after

  7. #7
    Registered User
    Join Date
    07-29-2013
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Copy and Paste Folder With VBA

    I have the same problem as Zorgunfleck!

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Copy and Paste Folder With VBA

    Hello mtu923 & Welcome to the Forum,

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    HTH
    Regards, Jeff

  9. #9
    Registered User
    Join Date
    09-22-2014
    Location
    India
    MS-Off Ver
    2010
    Posts
    1

    Re: Copy and Paste Folder With VBA

    I was trying to use Leith Ross's code. By exactly copying the code, how do I put source and destination path. Basically how to put runtime argument values.

  10. #10
    Forum Expert Fotis1991's Avatar
    Join Date
    10-11-2011
    Location
    Athens(The homeland of the Democracy!). Greece
    MS-Off Ver
    Excel 1997!&2003 & 2007&2010
    Posts
    13,744

    Re: Copy and Paste Folder With VBA

    Hello Samurai123 & Welcome to the Forum,

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Regards

    Fotis.

    -This is my Greek whisper to Europe.

    --Remember, saying thanks only takes a second or two. Click the little star * below, to give some Rep if you think an answer deserves it.

    Advanced Excel Techniques: http://excelxor.com/

    --KISS(Keep it simple Stupid)

    --Bring them back.

    ---See about Acropolis of Athens.

    --Visit Greece.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1