Consulting

Results 1 to 5 of 5

Thread: Copy, Rename and validate multiple excel files

  1. #1
    VBAX Newbie
    Joined
    Sep 2020
    Posts
    3
    Location

    Copy, Rename and validate multiple excel files

    Hi All, Been scratching around for the last 5 days to find something that works for multiple files. Many a late night/early hours of the morning unsuccessfully piecing together/coding to get a result. Thanks in advance.

    The following code is from get-digital-help.com/copyrename-a-file-excel-vba written by Oscar It works for 1 file, Ive got 8,000 files to do across a deep folder structure so I'd really like each row to look at a source path, source file name, destination path and destination file:

    For each row:
    Column A list the source path
    Column B lists the source file name
    Column C lists to destination path
    Column D lists the new file name

    Column E writes "Success" or "Fail" validation.
    - if file name already exists in destination, then "Fail"
    - If source file doesn't exist, then "Fail"


    Nice to have/completely optional!!!
    • Check if source file column A&B exists, = True or False record in column F. Where True, then proceed with copy and rename.


    • If destination file already exist, the fail and column F = duplicate
    • Leave the first row so I can insert column headers








    'Name macro
    Sub CopyRenameFile()
    
    'Dimension variables and declare data types
    Dim src As String, dst As String, fl As String
    Dim rfl As String
    
    'Save source directory specified in cell A2 to variable src
    src = Range("A2")
    
    'Save destination directory specified in cell C2 to variable dst
    dst = Range("C2")
    
    'Save file name specified in cell B2 to variable fl
    fl = Range("B2")
    
    'Save new file name specified in cell D2 to variable rfl
    rfl = Range("D2")
    
    'Enable error handling
    On Error Resume Next
    
    'Copy file based on variables src and fl to destination folder based on variable dst and name file based on value in rfl
    FileCopy src & "" & fl, dst & "" & rfl
    
    'Check if an error has occurred
    If Err.Number <> 0 Then
    
    'Show error using message box
    MsgBox "Copy error: " & src & "" & rfl
    End If
    
    'Disable error handling
    On Error GoTo 0
    
    End Sub
    Last edited by Paul_Hossler; 09-08-2020 at 05:51 PM.

  2. #2
    VBAX Newbie
    Joined
    Sep 2020
    Posts
    3
    Location
    Hi All, This problem is now resolved.
    Let me know if its allowed for the link to be pasted from another forum to ensure credit of the original person.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Glad you got any answer to your question

    Sure you can give credit to the other person

    BTW, the next time if you do post the question in any of the other forums, we like to know that so please post a link or links to the other forums

    You can read the FAQs at the link in my signature, especially the fourth item
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    VBAX Newbie
    Joined
    Sep 2020
    Posts
    3
    Location
    Thanks Paul.
    First time posting, so not sure of the do/dont's but will remember for next time.

  5. #5
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
      sn=sheet1.cells(1).currentregion.resize(,4)
      set sh= CreateObject("shell.application")
    
      for j=1 to ubound(sn)
        if dir (sn(j,1) & sn(j,2))<>"" then
          if dir(sn(j,3),16)="" then sh.Namespace(split(sn(j,3),":")(0)).NewFolder split(sn(j,3),":")(1)
          filecopy sn(j,1) & sn(j,2), sn(j,3) & sn(j,4)
        end if
      next
    End Sub

Tags for this Thread

Posting Permissions

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