+ Reply to Thread
Results 1 to 7 of 7

Macro to copy files referenced in hyperlinked cells to another folder

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Macro to copy files referenced in hyperlinked cells to another folder

    I am looking to copy files referenced as hyperlinks in an Excel worksheet to a directory specified in the macro. Attached is the Excel file (the spreadsheet is currently in the same directory as the referenced files).

    Ideally, I would like to select a range of cells with hyperlinks and only have those selected hyperlinked files copied over. I tried few scripts by Googling answers, but none seemed to work. Any ideas? Thanks..
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to copy files referenced in hyperlinked cells to another folder

    Sub copylistselectedfiles()
    oldpath = ThisWorkbook.Path
    newpath = "C:\test"
    For Each r In Selection
        FileCopy oldpath & r.Value, newpath & "\" & r
    Next
    End Sub
    If solved remember to mark Thread as solved

  3. #3
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Macro to copy files referenced in hyperlinked cells to another folder

    Made a slight correction to your script and go it to work. Thanks.

    Sub copylistselectedfiles()
    oldpath = ThisWorkbook.Path
    newpath = "C:\Test"
    For Each r In Selection
        FileCopy oldpath & "\" & r.Value, newpath & "\" & r
    Next
    End Sub

  4. #4
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Macro to copy files referenced in hyperlinked cells to another folder

    I have made some additional changes to the code for further refinement and it works, so long as the spreadsheet is saved in the same directory as the files it hyperlinks to.

    However, if the spreadsheet references files outside the current directory, it does not work (e.g. hyperlink show Travel/chicago.docx, with the cell showing chicago.docx). Any ideas how to modify it to work with file inside a directory as well as sub-directories? Thx..

    Latest code is below...


    Sub CopyListSelectedFiles()
    'Copy selected hyperlinked files to selected destination folder
    Dim oldpath As String
    Dim newpath As String
    Dim rng As Range
    Dim r As Object
    oldpath = ActiveWorkbook.path
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show
       On Error Resume Next
       newpath = .SelectedItems(1)
       err.Clear
       On Error GoTo 0
     End With
     On Error Resume Next
    For Each r In rng
        FileCopy oldpath & "\" & r.Value, newpath & "\" & r
    Next
    MsgBox "Files Copied to " & newpath
    End Sub

  5. #5
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: Macro to copy files referenced in hyperlinked cells to another folder

    attach please a new sample file for testing

  6. #6
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 2016 on Win10 (notebook), private: 365 on Win11 (desktop), 2019 on Win11 (notebook)
    Posts
    8,198

    Re: Macro to copy files referenced in hyperlinked cells to another folder

    Hi, Brawnystaff,

    from what I understand: maybe alter the hyperlinks to point to the new directory as they still should point to the old directory?

    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  7. #7
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 365
    Posts
    266

    Re: Macro to copy files referenced in hyperlinked cells to another folder

    Thanks HaHoBe..rather than put the path in each hyperlink, I just put it in cell A1 and reference it in the macro. Updated code below. Will mark this as solved..

    Sub CopyListSelectedFiles()
    'Copy selected hyperlinked files to selected destination folder
    Dim oldpath As String
    Dim newpath As String
    Dim rng As Range
    Dim r As Object
    oldpath = Range("A1")
    If InStr(1, oldpath, "\", vbTextCompare) Then
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    Else
    MsgBox "Ensure cell A1 has path of files" & vbNewLine & "e.g. C:\My Files\"
    GoTo Nope
    End If
    With Application.FileDialog(msoFileDialogFolderPicker)
       .AllowMultiSelect = False
       .Show
       On Error Resume Next
       newpath = .SelectedItems(1)
       err.Clear
       On Error GoTo 0
     End With
     On Error Resume Next
    For Each r In rng
        FileCopy oldpath & r.Value, newpath & "\" & r
    Next
    MsgBox "Selected Files Copied to " & newpath
    Nope:
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy List of Hyperlinked Files From Source to Target Folder
    By ugadawgs311 in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 04-01-2014, 03:21 AM
  2. Macro to copy the value of some cells in excel files in a folder to a master file
    By phemocheee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-17-2013, 08:33 AM
  3. Macro to copy some cells in some excel files in a folder to a master file
    By phemocheee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-17-2013, 04:40 AM
  4. [SOLVED] VBA Code open files in folder, copy text to workbook-Next time folder opened copy only new
    By Bikeman in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-02-2013, 07:59 PM
  5. Replies: 0
    Last Post: 10-27-2011, 06:46 AM

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